Stern-Brocot sequence: Difference between revisions
Content added Content deleted
ReeceGoding (talk | contribs) m (→While loop: Improved syntax.) |
(Stern-Brocot sequence in various BASIC dialents) |
||
Line 1,520: | Line 1,520: | ||
All GCDs are 1. |
All GCDs are 1. |
||
</pre> |
</pre> |
||
==={{header|BASIC256}}=== |
|||
{{trans|FreeBASIC}} |
|||
<lang basic256>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</lang> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|QBasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
{{works with|QBasic|1.1}} |
|||
<lang QBasic>CONST max = 2000 |
|||
DIM SHARED stern(max + 2) |
|||
FUNCTION gcd (x, y) |
|||
WHILE y |
|||
t = y |
|||
y = x MOD y |
|||
x = t |
|||
WEND |
|||
gcd = x |
|||
END FUNCTION |
|||
SUB SternBrocot |
|||
stern(1) = 1 |
|||
stern(2) = 1 |
|||
i = 2: n = 2: ub = UBOUND(stern) |
|||
DO WHILE i < ub |
|||
i = i + 1 |
|||
stern(i) = stern(n) + stern(n - 1) |
|||
i = i + 1 |
|||
stern(i) = stern(n) |
|||
n = n + 1 |
|||
LOOP |
|||
END SUB |
|||
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 USING " ######"; i; stern(i) |
|||
d = 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</lang> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<lang yabasic>limite = 2000 |
|||
dim stern(limite+2) |
|||
sub SternBrocot() |
|||
stern(1) = 1 |
|||
stern(2) = 1 |
|||
i = 2 : n = 2 : ub = arraysize(stern(),1) |
|||
while i < ub |
|||
i = i + 1 |
|||
stern(i) = stern(n) + stern(n -1) |
|||
i = i + 1 |
|||
stern(i) = stern(n) |
|||
n = n + 1 |
|||
wend |
|||
end sub |
|||
sub gcd(p, q) |
|||
if q = 0 return p |
|||
return gcd(q, mod(p, q)) |
|||
end sub |
|||
SternBrocot() |
|||
print "The first 15 are: "; |
|||
for i = 1 to 15 |
|||
print stern(i), " "; |
|||
next i |
|||
print "\n\n Index First nr." |
|||
d = 1 |
|||
for i = 1 to limite |
|||
if stern(i) = d then |
|||
print i using "######", stern(i) using "######" |
|||
d = d + 1 |
|||
if d = 11 d = 100 |
|||
if d = 101 break |
|||
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)) |
|||
break |
|||
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</lang> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |