Stern-Brocot sequence: Difference between revisions

Added 11l
(Added 11l)
Line 42:
* [https://oeis.org/A002487 A002487] The On-Line Encyclopedia of Integer Sequences.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>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’)</lang>
 
{{out}}
<pre>
The first 15 values:
[1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4]
 
1-based index of the first occurrence of 1 in the series: 1
1-based index of the first occurrence of 2 in the series: 3
1-based index of the first occurrence of 3 in the series: 5
1-based index of the first occurrence of 4 in the series: 9
1-based index of the first occurrence of 5 in the series: 11
1-based index of the first occurrence of 6 in the series: 33
1-based index of the first occurrence of 7 in the series: 19
1-based index of the first occurrence of 8 in the series: 21
1-based index of the first occurrence of 9 in the series: 35
1-based index of the first occurrence of 10 in the series: 39
1-based index of the first occurrence of 100 in the series: 1179
</pre>
 
=={{header|360 Assembly}}==
1,463

edits