38 lines
891 B
Plaintext
38 lines
891 B
Plaintext
// Rosetta Code problem: https://rosettacode.org/wiki/Farey_sequence
|
|
// by Jjuanhdez, 06/2022
|
|
|
|
for i = 1 to 11
|
|
print "F", i, " = ";
|
|
farey(i, FALSE)
|
|
next i
|
|
print
|
|
for i = 100 to 1000 step 100
|
|
print "F", i;
|
|
if i <> 1000 then print " "; else print ""; : fi
|
|
print " = ";
|
|
farey(i, FALSE)
|
|
next i
|
|
end
|
|
|
|
sub farey(n, descending)
|
|
a = 0 : b = 1 : c = 1 : d = n : k = 0
|
|
cont = 0
|
|
|
|
if descending = TRUE then
|
|
a = 1 : c = n -1
|
|
end if
|
|
|
|
cont = cont + 1
|
|
if n < 12 then print a, "/", b, " "; : fi
|
|
|
|
while ((c <= n) and not descending) or ((a > 0) and descending)
|
|
aa = a : bb = b : cc = c : dd = d
|
|
k = int((n + b) / d)
|
|
a = cc : b = dd : c = k * cc - aa : d = k * dd - bb
|
|
cont = cont + 1
|
|
if n < 12 then print a, "/", b, " "; : fi
|
|
end while
|
|
|
|
if n < 12 then print else print cont using("######") : fi
|
|
end sub
|