Calkin-Wilf sequence: Difference between revisions

Added 11l
(add BQN)
(Added 11l)
Line 35:
* [[Continued fraction/Arithmetic/Construct from rational number]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<lang 11l>T CalkinWilf
n = 1
d = 1
 
F ()()
V r = (.n, .d)
.n = 2 * (.n I/ .d) * .d + .d - .n
swap(&.n, &.d)
R r
 
print(‘The first 20 terms of the Calkwin-Wilf sequence are:’)
V cw = CalkinWilf()
[String] seq
L 20
V n = cw()
seq.append(I n[1] == 1 {String(n[0])} E n[0]‘/’n[1])
print(seq.join(‘, ’))
 
cw = CalkinWilf()
V index = 1
L cw() != (83116, 51639)
index++
print("\nThe element 83116/51639 is at position "index‘ in the sequence.’)</lang>
 
{{out}}
<pre>
The first 20 terms of the Calkwin-Wilf sequence are:
1, 1/2, 2, 1/3, 3/2, 2/3, 3, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4, 1/5, 5/4, 4/7, 7/3, 3/8
 
The element 83116/51639 is at position 123456789 in the sequence.
</pre>
 
=={{header|AppleScript}}==
1,480

edits