RosettaCodeData/Task/Stern-Brocot-sequence/BASIC256/stern-brocot-sequence.basic

64 lines
951 B
Plaintext

arraybase 1
max = 2000
global stern
dim stern(max+2)
subroutine SternBrocot()
stern[1] = 1
stern[2] = 1
i = 2 : n = 2 : ub = stern[?]
while i < ub
i += 1
stern[i] = stern[n] + stern[n-1]
i += 1
stern[i] = stern[n]
n += 1
end while
end subroutine
function gcd(x, y)
while y
t = y
y = x mod y
x = t
end while
gcd = x
end function
call SternBrocot()
print "The first 15 are: ";
for i = 1 to 15
print stern[i]; " ";
next i
print : print
print "Index First nr."
d = 1
for i = 1 to max
if stern[i] = d then
print i; chr(9); stern[i]
d += 1
if d = 11 then d = 100
if d = 101 then exit for
i = 0
end if
next i
print : print
d = 0
for i = 1 to 1000
if gcd(stern[i], stern[i+1]) <> 1 then
d = gcd(stern[i], stern[i+1])
exit for
end if
next i
if d = 0 then
print "GCD of two consecutive members of the series up to the 1000th member is 1"
else
print "The GCD for index "; i; " and "; i+1; " = "; d
end if