RosettaCodeData/Task/Stern-Brocot-sequence/11l/stern-brocot-sequence.11l
2023-07-01 13:44:08 -04:00

20 lines
731 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

F stern_brocot(predicate = series -> series.len < 20)
V sb = [1, 1]
V i = 0
L predicate(sb)
sb [+]= [sum(sb[i .< i + 2]), sb[i + 1]]
i++
R sb
V n_first = 15
print(("The first #. values:\n ".format(n_first)) stern_brocot(series -> series.len < :n_first)[0 .< n_first])
print()
V n_max = 10
L(n_occur) Array(1 .. n_max) [+] [100]
print((1-based index of the first occurrence of #3 in the series:.format(n_occur)) (stern_brocot(series -> @n_occur !C series).index(n_occur) + 1))
print()
V n_gcd = 1000
V s = stern_brocot(series -> series.len < :n_gcd)[0 .< n_gcd]
assert(all(zip(s, s[1..]).map((prev, this) -> gcd(prev, this) == 1)), A fraction from adjacent terms is reducible)