Calkin-Wilf sequence: Difference between revisions
Content added Content deleted
(Added a Scheme implementation.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 39: | Line 39: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang=11l>T CalkinWilf |
||
n = 1 |
n = 1 |
||
d = 1 |
d = 1 |
||
Line 61: | Line 61: | ||
L cw() != (83116, 51639) |
L cw() != (83116, 51639) |
||
index++ |
index++ |
||
print("\nThe element 83116/51639 is at position "index‘ in the sequence.’)</ |
print("\nThe element 83116/51639 is at position "index‘ in the sequence.’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 73: | Line 73: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Uses code from the [[Arithmetic/Rational]] and [[Continued fraction/Arithmetic/Construct from rational number]] tasks. |
Uses code from the [[Arithmetic/Rational]] and [[Continued fraction/Arithmetic/Construct from rational number]] tasks. |
||
< |
<syntaxhighlight lang=algol68>BEGIN |
||
# Show elements 1-20 of the Calkin-Wilf sequence as rational numbers # |
# Show elements 1-20 of the Calkin-Wilf sequence as rational numbers # |
||
# also show the position of a specific element in the seuence # |
# also show the position of a specific element in the seuence # |
||
Line 203: | Line 203: | ||
) |
) |
||
END |
END |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 212: | Line 212: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang=applescript>-- Return the first n terms of the sequence. Tree generation. Faster for this purpose. |
||
on CalkinWilfSequence(n) |
on CalkinWilfSequence(n) |
||
script o |
script o |
||
Line 309: | Line 309: | ||
set AppleScript's text item delimiters to astid |
set AppleScript's text item delimiters to astid |
||
set output to output & (linefeed & "83116/51639 is term number " & positionResult) |
set output to output & (linefeed & "83116/51639 is term number " & positionResult) |
||
return output</ |
return output</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang=applescript>"First twenty terms of sequence using tree generation: |
||
1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, 1/5, 5/4, 4/7, 7/3, 3/8 |
1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, 1/5, 5/4, 4/7, 7/3, 3/8 |
||
Ditto using binary run-length encoding: |
Ditto using binary run-length encoding: |
||
1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, 1/5, 5/4, 4/7, 7/3, 3/8 |
1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, 1/5, 5/4, 4/7, 7/3, 3/8 |
||
83116/51639 is term number 123456789"</ |
83116/51639 is term number 123456789"</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang=rebol>n: new 1 |
||
d: new 1 |
d: new 1 |
||
calkinWilf: function [] .export:[n,d] [ |
calkinWilf: function [] .export:[n,d] [ |
||
Line 346: | Line 346: | ||
print "" |
print "" |
||
print ["The element" ~"|target\0|/|target\1|" "is at position" indx "in the sequence."]</ |
print ["The element" ~"|target\0|/|target\1|" "is at position" indx "in the sequence."]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 362: | Line 362: | ||
<code>GCD</code> and <code>_while_</code> are idioms from [https://mlochbaum.github.io/bqncrate/ BQNcrate]. |
<code>GCD</code> and <code>_while_</code> are idioms from [https://mlochbaum.github.io/bqncrate/ BQNcrate]. |
||
< |
<syntaxhighlight lang=bqn>GCD ← {m 𝕊⍟(0<m←𝕨|𝕩) 𝕨} |
||
_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩} |
_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩} |
||
Sim ← { # Simplify a fraction |
Sim ← { # Simplify a fraction |
||
Line 386: | Line 386: | ||
cnt‿fr: |
cnt‿fr: |
||
fr ≢ 83116‿51639 |
fr ≢ 83116‿51639 |
||
} ⟨1,1‿1⟩</ |
} ⟨1,1‿1⟩</syntaxhighlight> |
||
< |
<syntaxhighlight lang=bqn>⟨ ⟨ 1 2 ⟩ ⟨ 2 1 ⟩ ⟨ 1 3 ⟩ ⟨ 3 2 ⟩ ⟨ 2 3 ⟩ ⟨ 3 1 ⟩ ⟨ 1 4 ⟩ ⟨ 4 3 ⟩ ⟨ 3 5 ⟩ ⟨ 5 2 ⟩ ⟨ 2 5 ⟩ ⟨ 5 3 ⟩ ⟨ 3 4 ⟩ ⟨ 4 1 ⟩ ⟨ 1 5 ⟩ ⟨ 5 4 ⟩ ⟨ 4 7 ⟩ ⟨ 7 3 ⟩ ⟨ 3 8 ⟩ ⟨ 8 5 ⟩ ⟩ |
||
⟨ 123456789 ⟨ 83116 51639 ⟩ ⟩</ |
⟨ 123456789 ⟨ 83116 51639 ⟩ ⟩</syntaxhighlight> |
||
You can try Part 1 [https://mlochbaum.github.io/BQN/try.html#code=R0NEIOKGkCB7bSDwnZWK4o2fKDA8beKGkPCdlah88J2VqSkg8J2VqH0KX3doaWxlXyDihpAge/CdlL3ijZ/wnZS+4oiY8J2UvV/wnZWjX/CdlL7iiJjwnZS94o2f8J2UvvCdlal9ClNpbSDihpAgewogIHjwnZWKMTog8J2VqOKAvzE7CiAgMPCdlYp5OiAw4oC/8J2VqTsKICDijIrwnZWo4oC/8J2VqSDDtyDwnZWoIEdDRCDwnZWpCn0KQWRkIOKGkCB7CiAgMOKAv2Ig8J2ViiDwnZWpOiDwnZWpOwogIPCdlagg8J2ViiAw4oC/eTog8J2VqDsKICBh4oC/YiDwnZWKIHjigL95OgogICgoYcOXeSkreMOXYikgU2ltIGLDl3kKfQpOZXh0IOKGkCB7buKAv2Q6IOKMvSgyw5fijIrDt8K0buKAv2Qp4oC/MSBBZGQgKGQtbinigL9kfQpDYWwg4oaQIHtOZXh04o2f8J2VqSAx4oC/MX0KCuKAolNob3cgQ2FsIDEr4oaVMjA= here.] Second part can and will hang your browser, so it is best to try locally on [[CBQN]]. |
You can try Part 1 [https://mlochbaum.github.io/BQN/try.html#code=R0NEIOKGkCB7bSDwnZWK4o2fKDA8beKGkPCdlah88J2VqSkg8J2VqH0KX3doaWxlXyDihpAge/CdlL3ijZ/wnZS+4oiY8J2UvV/wnZWjX/CdlL7iiJjwnZS94o2f8J2UvvCdlal9ClNpbSDihpAgewogIHjwnZWKMTog8J2VqOKAvzE7CiAgMPCdlYp5OiAw4oC/8J2VqTsKICDijIrwnZWo4oC/8J2VqSDDtyDwnZWoIEdDRCDwnZWpCn0KQWRkIOKGkCB7CiAgMOKAv2Ig8J2ViiDwnZWpOiDwnZWpOwogIPCdlagg8J2ViiAw4oC/eTog8J2VqDsKICBh4oC/YiDwnZWKIHjigL95OgogICgoYcOXeSkreMOXYikgU2ltIGLDl3kKfQpOZXh0IOKGkCB7buKAv2Q6IOKMvSgyw5fijIrDt8K0buKAv2Qp4oC/MSBBZGQgKGQtbinigL9kfQpDYWwg4oaQIHtOZXh04o2f8J2VqSAx4oC/MX0KCuKAolNob3cgQ2FsIDEr4oaVMjA= here.] Second part can and will hang your browser, so it is best to try locally on [[CBQN]]. |
||
Line 394: | Line 394: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=bracmat>( 1:?a |
||
& 0:?i |
& 0:?i |
||
& whl |
& whl |
||
Line 427: | Line 427: | ||
) |
) |
||
& out$(get-term-num$83116/51639) |
& out$(get-term-num$83116/51639) |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1/2 |
<pre>1/2 |
||
Line 452: | Line 452: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
#include <vector> |
#include <vector> |
||
#include <boost/rational.hpp> |
#include <boost/rational.hpp> |
||
Line 504: | Line 504: | ||
rational r(83116, 51639); |
rational r(83116, 51639); |
||
std::cout << r << " is the " << term_number(r) << "th term of the sequence.\n"; |
std::cout << r << " is the " << term_number(r) << "th term of the sequence.\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 535: | Line 535: | ||
===Find first n terms=== |
===Find first n terms=== |
||
{{trans|Pascal}} |
{{trans|Pascal}} |
||
< |
<syntaxhighlight lang=edsac> |
||
[For Rosetta Code. EDSAC program, Initial Orders 2. |
[For Rosetta Code. EDSAC program, Initial Orders 2. |
||
Prints the first 20 terms of the Calkin-Wilf sequence. |
Prints the first 20 terms of the Calkin-Wilf sequence. |
||
Line 616: | Line 616: | ||
P F [enter with acc = 0] |
P F [enter with acc = 0] |
||
[end] |
[end] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 642: | Line 642: | ||
===Find index of a given term=== |
===Find index of a given term=== |
||
{{trans|Pascal}} |
{{trans|Pascal}} |
||
< |
<syntaxhighlight lang=edsac> |
||
[For Rosetta Code. EDSAC program, Initial Orders 2.] |
[For Rosetta Code. EDSAC program, Initial Orders 2.] |
||
[Finds the index of a given rational in the Calkin-Wilf series.] |
[Finds the index of a given rational in the Calkin-Wilf series.] |
||
Line 754: | Line 754: | ||
E 19 Z [relative address of entry point] |
E 19 Z [relative address of entry point] |
||
P F [enter with acc = 0] |
P F [enter with acc = 0] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 762: | Line 762: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
===The Function=== |
===The Function=== |
||
< |
<syntaxhighlight lang=fsharp> |
||
// Calkin Wilf Sequence. Nigel Galloway: January 9th., 2021 |
// Calkin Wilf Sequence. Nigel Galloway: January 9th., 2021 |
||
let cW=Seq.unfold(fun(n)->Some(n,seq{for n,g in n do yield (n,n+g); yield (n+g,g)}))(seq[(1,1)])|>Seq.concat |
let cW=Seq.unfold(fun(n)->Some(n,seq{for n,g in n do yield (n,n+g); yield (n+g,g)}))(seq[(1,1)])|>Seq.concat |
||
</syntaxhighlight> |
|||
</lang> |
|||
===The Tasks=== |
===The Tasks=== |
||
; first 20 |
; first 20 |
||
< |
<syntaxhighlight lang=fsharp> |
||
cW |> Seq.take 20 |> Seq.iter(fun(n,g)->printf "%d/%d " n g);printfn "" |
cW |> Seq.take 20 |> Seq.iter(fun(n,g)->printf "%d/%d " n g);printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 776: | Line 776: | ||
</pre> |
</pre> |
||
; Indexof 83116/51639 |
; Indexof 83116/51639 |
||
< |
<syntaxhighlight lang=fsharp> |
||
printfn "%d" (1+Seq.findIndex(fun n->n=(83116,51639)) cW) |
printfn "%d" (1+Seq.findIndex(fun n->n=(83116,51639)) cW) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 785: | Line 785: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2020-08-14}} |
{{works with|Factor|0.99 2020-08-14}} |
||
< |
<syntaxhighlight lang=factor>USING: formatting io kernel lists lists.lazy math |
||
math.continued-fractions math.functions math.parser prettyprint |
math.continued-fractions math.functions math.parser prettyprint |
||
sequences strings vectors ; |
sequences strings vectors ; |
||
Line 805: | Line 805: | ||
20 calkin-wilf ltake [ pprint bl ] leach nl nl |
20 calkin-wilf ltake [ pprint bl ] leach nl nl |
||
83116/51639 cw-index "83116/51639 is at index %d.\n" printf</ |
83116/51639 cw-index "83116/51639 is at index %d.\n" printf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 819: | Line 819: | ||
{{works with|gforth|0.7.3}} |
{{works with|gforth|0.7.3}} |
||
< |
<syntaxhighlight lang=forth>\ Calkin-Wilf sequence |
||
: frac. swap . ." / " . ; |
: frac. swap . ." / " . ; |
||
Line 852: | Line 852: | ||
20 cw-seq |
20 cw-seq |
||
cr 83116 51639 2dup frac. cw-index index @ . ; |
cr 83116 51639 2dup frac. cw-index index @ . ; |
||
cw-demo</ |
cw-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 883: | Line 883: | ||
Uses the code from [[Greatest common divisor#FreeBASIC]] as an include. |
Uses the code from [[Greatest common divisor#FreeBASIC]] as an include. |
||
< |
<syntaxhighlight lang=freebasic>#include "gcd.bas" |
||
type rational |
type rational |
||
Line 990: | Line 990: | ||
q.num = 83116 |
q.num = 83116 |
||
q.den = 51639 |
q.den = 51639 |
||
print disp_rational(q)+" is the "+str(frac_to_int(q))+"th term."</ |
print disp_rational(q)+" is the "+str(frac_to_int(q))+"th term."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,027: | Line 1,027: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
Go just has arbitrary precision rational numbers which we use here whilst assuming the numbers needed for this task can be represented exactly by the 64 bit built-in types. |
Go just has arbitrary precision rational numbers which we use here whilst assuming the numbers needed for this task can be represented exactly by the 64 bit built-in types. |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 1,117: | Line 1,117: | ||
tn := getTermNumber(cf) |
tn := getTermNumber(cf) |
||
fmt.Printf("%s is the %sth term of the sequence.\n", r.RatString(), commatize(tn)) |
fmt.Printf("%s is the %sth term of the sequence.\n", r.RatString(), commatize(tn)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,147: | Line 1,147: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=haskell>import Control.Monad (forM_) |
||
import Data.Bool (bool) |
import Data.Bool (bool) |
||
import Data.List.NonEmpty (NonEmpty, fromList, toList, unfoldr) |
import Data.List.NonEmpty (NonEmpty, fromList, toList, unfoldr) |
||
Line 1,201: | Line 1,201: | ||
"\n%s is at index %d of the Calkin-Wilf sequence.\n" |
"\n%s is at index %d of the Calkin-Wilf sequence.\n" |
||
(show r) |
(show r) |
||
(calkinWilfIdx r)</ |
(calkinWilfIdx r)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,239: | Line 1,239: | ||
</pre> |
</pre> |
||
given definitions |
given definitions |
||
<syntaxhighlight lang=J> |
|||
<lang J> |
|||
cw_next_term=: [: % +:@<. + -. |
cw_next_term=: [: % +:@<. + -. |
||
Line 1,261: | Line 1,261: | ||
NB. base 2 @ reverse @ the cf's representation copies of 1 0 1 0 ... |
NB. base 2 @ reverse @ the cf's representation copies of 1 0 1 0 ... |
||
index_cw_term=: #.@|.@(# 1 0 $~ #)@molcf@ccf |
index_cw_term=: #.@|.@(# 1 0 $~ #)@molcf@ccf |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang=julia>function calkin_wilf(n) |
||
cw = zeros(Rational, n + 1) |
cw = zeros(Rational, n + 1) |
||
for i in 2:n + 1 |
for i in 2:n + 1 |
||
Line 1,301: | Line 1,301: | ||
const tn = term_number(cf) |
const tn = term_number(cf) |
||
println("$r is the $tn-th term of the sequence.") |
println("$r is the $tn-th term of the sequence.") |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
The first 20 terms of the Calkin-Wilf sequence are: Rational[1//1, 1//2, 2//1, 1//3, 3//2, 2//3, 3//1, 1//4, 4//3, 3//5, 5//2, 2//5, 5//3, 3//4, 4//1, 1//5, 5//4, 4//7, 7//3, 3//8] |
The first 20 terms of the Calkin-Wilf sequence are: Rational[1//1, 1//2, 2//1, 1//3, 3//2, 2//3, 3//1, 1//4, 4//3, 3//5, 5//2, 2//5, 5//3, 3//4, 4//1, 1//5, 5//4, 4//7, 7//3, 3//8] |
||
Line 1,311: | Line 1,311: | ||
===Find first n terms=== |
===Find first n terms=== |
||
{{trans|Pascal}} |
{{trans|Pascal}} |
||
< |
<syntaxhighlight lang=Little Man Computer> |
||
// Little Man Computer, for Rosetta Code. |
// Little Man Computer, for Rosetta Code. |
||
// Displays terms of Calkin-Wilf sequence up to the given index. |
// Displays terms of Calkin-Wilf sequence up to the given index. |
||
Line 1,402: | Line 1,402: | ||
b DAT |
b DAT |
||
// end |
// end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,429: | Line 1,429: | ||
{{trans|Pascal}} |
{{trans|Pascal}} |
||
The numbers in part 2 of the task are too large for LMC, so the demo program just confirms the example, that 9/4 is the 35th term. |
The numbers in part 2 of the task are too large for LMC, so the demo program just confirms the example, that 9/4 is the 35th term. |
||
< |
<syntaxhighlight lang=Little Man Computer> |
||
// Little Man Computer, for Rosetta Code. |
// Little Man Computer, for Rosetta Code. |
||
// Calkin-Wilf sequence: displays index of term entered by user. |
// Calkin-Wilf sequence: displays index of term entered by user. |
||
Line 1,496: | Line 1,496: | ||
index DAT |
index DAT |
||
// end |
// end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,504: | Line 1,504: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>ClearAll[a] |
||
a[1] = 1; |
a[1] = 1; |
||
a[n_?(GreaterThan[1])] := a[n] = 1/(2 Floor[a[n - 1]] + 1 - a[n - 1]) |
a[n_?(GreaterThan[1])] := a[n] = 1/(2 Floor[a[n - 1]] + 1 - a[n - 1]) |
||
Line 1,521: | Line 1,521: | ||
Break[]; |
Break[]; |
||
] |
] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{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} |
<pre>{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} |
||
Line 1,530: | Line 1,530: | ||
With these optimizations, the program runs in less than 1.3 s on our laptop. |
With these optimizations, the program runs in less than 1.3 s on our laptop. |
||
< |
<syntaxhighlight lang=Nim>type Fraction = tuple[num, den: uint32] |
||
iterator calkinWilf(): Fraction = |
iterator calkinWilf(): Fraction = |
||
Line 1,564: | Line 1,564: | ||
inc index |
inc index |
||
if an == Target: break |
if an == Target: break |
||
echo "\nThe element ", $Target, " is at position ", $index, " in the sequence."</ |
echo "\nThe element ", $Target, " is at position ", $index, " in the sequence."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,574: | Line 1,574: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
These programs were written in Free Pascal, using the Lazarus IDE and the Free Pascal compiler version 3.2.0. They are based on the Wikipedia article "Calkin-Wilf tree", rather than the algorithms in the task description. |
These programs were written in Free Pascal, using the Lazarus IDE and the Free Pascal compiler version 3.2.0. They are based on the Wikipedia article "Calkin-Wilf tree", rather than the algorithms in the task description. |
||
< |
<syntaxhighlight lang=pascal> |
||
program CWTerms; |
program CWTerms; |
||
Line 1,695: | Line 1,695: | ||
WriteLn( SysUtils.Format( '%8d: %d/%d', [k, Num, Den])); |
WriteLn( SysUtils.Format( '%8d: %d/%d', [k, Num, Den])); |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,719: | Line 1,719: | ||
20: 3/8 |
20: 3/8 |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang=pascal> |
||
program CWIndex; |
program CWIndex; |
||
Line 1,770: | Line 1,770: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,779: | Line 1,779: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang=perl>use strict; |
||
use warnings; |
use warnings; |
||
use feature qw(say state); |
use feature qw(say state); |
||
Line 1,811: | Line 1,811: | ||
say 'First twenty terms of the Calkin-Wilf sequence:'; |
say 'First twenty terms of the Calkin-Wilf sequence:'; |
||
printf "%s ", $calkin_wilf->next() for 1..20; |
printf "%s ", $calkin_wilf->next() for 1..20; |
||
say "\n\n83116/51639 is at index: " . r2cw(83116,51639);</ |
say "\n\n83116/51639 is at index: " . r2cw(83116,51639);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First twenty terms of the Calkin-Wilf sequence: |
<pre>First twenty terms of the Calkin-Wilf sequence: |
||
Line 1,819: | Line 1,819: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang=Phix>(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (new even() builtin)</span> |
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (new even() builtin)</span> |
||
Line 1,910: | Line 1,910: | ||
<span style="color: #004080;">integer</span> <span style="color: #000000;">tn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_term_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cf</span><span style="color: #0000FF;">)</span> |
<span style="color: #004080;">integer</span> <span style="color: #000000;">tn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_term_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cf</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d/%d is the %,d%s term of the sequence.\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">&{</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d/%d is the %,d%s term of the sequence.\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">&{</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">)})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,939: | Line 1,939: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang=prolog> |
||
% John Devou: 26-Nov-2021 |
% John Devou: 26-Nov-2021 |
||
Line 1,953: | Line 1,953: | ||
t(A/B,S,C,X):- B > 1, divmod(A,B,M,N), T is 1-S, D is C*2**M, t(B/N,T,D,Y), X is Y + S*C*(2**M-1). |
t(A/B,S,C,X):- B > 1, divmod(A,B,M,N), T is 1-S, D is C*2**M, t(B/N,T,D,Y), X is Y + S*C*(2**M-1). |
||
t(A/B,X):- t(A/B,1,1,X), !. |
t(A/B,X):- t(A/B,1,1,X), !. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,965: | Line 1,965: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang=python>from fractions import Fraction |
||
from math import floor |
from math import floor |
||
from itertools import islice, groupby |
from itertools import islice, groupby |
||
Line 1,995: | Line 1,995: | ||
print('TERMS 1..20: ', ', '.join(str(x) for x in islice(cw(), 20))) |
print('TERMS 1..20: ', ', '.join(str(x) for x in islice(cw(), 20))) |
||
x = Fraction(83116, 51639) |
x = Fraction(83116, 51639) |
||
print(f"\n{x} is the {get_term_num(x):_}'th term.")</ |
print(f"\n{x} is the {get_term_num(x):_}'th term.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,006: | Line 2,006: | ||
<code>cf</code> is defined at [[Continued fraction/Arithmetic/Construct from rational number#Quackery]]. |
<code>cf</code> is defined at [[Continued fraction/Arithmetic/Construct from rational number#Quackery]]. |
||
< |
<syntaxhighlight lang=Quackery> [ $ "bigrat.qky" loadfile ] now! |
||
[ ' [ [ 1 1 ] ] |
[ ' [ [ 1 1 ] ] |
||
Line 2,036: | Line 2,036: | ||
[ do vulgar$ echo$ sp ] |
[ do vulgar$ echo$ sp ] |
||
cr cr |
cr cr |
||
83116 51639 cw-term echo</ |
83116 51639 cw-term echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,049: | Line 2,049: | ||
This implementation includes a bogus undefined value at position 0, having the bogus first term shifts the indices up by one, making the ordinal position and index match. Useful due to how reversibility function works. |
This implementation includes a bogus undefined value at position 0, having the bogus first term shifts the indices up by one, making the ordinal position and index match. Useful due to how reversibility function works. |
||
<lang |
<syntaxhighlight lang=raku line>my @calkin-wilf = Any, 1, {1 / (.Int × 2 + 1 - $_)} … *; |
||
# Rational to Calkin-Wilf index |
# Rational to Calkin-Wilf index |
||
Line 2,080: | Line 2,080: | ||
return $num.numerator if $num.denominator == 1; |
return $num.numerator if $num.denominator == 1; |
||
$num.nude.join: '/'; |
$num.nude.join: '/'; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First twenty terms of the Calkin-Wilf sequence: 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 |
<pre>First twenty terms of the Calkin-Wilf sequence: 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 |
||
Line 2,093: | Line 2,093: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The meat of this REXX program was provided by Paul Kislanko. |
The meat of this REXX program was provided by Paul Kislanko. |
||
< |
<syntaxhighlight lang=rexx>/*REXX pgm finds the Nth value of the Calkin─Wilf sequence (which will be a fraction),*/ |
||
/*────────────────────── or finds which sequence number contains a specified fraction). */ |
/*────────────────────── or finds which sequence number contains a specified fraction). */ |
||
numeric digits 2000 /*be able to handle ginormic integers. */ |
numeric digits 2000 /*be able to handle ginormic integers. */ |
||
Line 2,142: | Line 2,142: | ||
obin= copies(1, f1)copies(0, f0)obin |
obin= copies(1, f1)copies(0, f0)obin |
||
end /*until*/ |
end /*until*/ |
||
return x2d( b2x(obin) ) /*RLE2DEC: Run Length Encoding ──► decimal*/</ |
return x2d( b2x(obin) ) /*RLE2DEC: Run Length Encoding ──► decimal*/</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 2,153: | Line 2,153: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=ruby>cw = Enumerator.new do |y| |
||
y << a = 1.to_r |
y << a = 1.to_r |
||
loop { y << a = 1/(2*a.floor + 1 - a) } |
loop { y << a = 1/(2*a.floor + 1 - a) } |
||
Line 2,173: | Line 2,173: | ||
puts cw.take(20).join(", ") |
puts cw.take(20).join(", ") |
||
puts term_num (83116/51639r) |
puts term_num (83116/51639r) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre>1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, 1/5, 5/4, 4/7, 7/3, 3/8 |
<pre>1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, 4/1, 1/5, 5/4, 4/7, 7/3, 3/8 |
||
123456789 |
123456789 |
||
Line 2,179: | Line 2,179: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang=rust>// [dependencies] |
||
// num = "0.3" |
// num = "0.3" |
||
Line 2,232: | Line 2,232: | ||
let r = Rational::new(83116, 51639); |
let r = Rational::new(83116, 51639); |
||
println!("{} is the {}th term of the sequence.", r, term_number(&r)); |
println!("{} is the {}th term of the sequence.", r, term_number(&r)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,262: | Line 2,262: | ||
{{works with|Chez Scheme}} |
{{works with|Chez Scheme}} |
||
'''Continued Fraction support''' |
'''Continued Fraction support''' |
||
< |
<syntaxhighlight lang=scheme>; Create a terminating Continued Fraction generator for the given rational number. |
||
; Returns one term per call; returns #f when no more terms remaining. |
; Returns one term per call; returns #f when no more terms remaining. |
||
(define make-continued-fraction-gen |
(define make-continued-fraction-gen |
||
Line 2,295: | Line 2,295: | ||
(set-car! cf-last-cons (1- (car cf-last-cons))) |
(set-car! cf-last-cons (1- (car cf-last-cons))) |
||
(set-cdr! cf-last-cons (cons 1 '())))) |
(set-cdr! cf-last-cons (cons 1 '())))) |
||
cf))</ |
cf))</syntaxhighlight> |
||
'''Calkin-Wilf sequence''' |
'''Calkin-Wilf sequence''' |
||
< |
<syntaxhighlight lang=scheme>; Create a Calkin-Wilf sequence generator. |
||
(define make-calkin-wilf-gen |
(define make-calkin-wilf-gen |
||
(lambda () |
(lambda () |
||
Line 2,331: | Line 2,331: | ||
; Return the run-length binary encoding from the odd-length Calkin-Wilf sequence of the |
; Return the run-length binary encoding from the odd-length Calkin-Wilf sequence of the |
||
; given rational number. This is equal to the number's position in the sequence. |
; given rational number. This is equal to the number's position in the sequence. |
||
(encode-list-of-runs 0 1 (continued-fraction-list-enforce-odd-length! (rat->cf-list rat)))))</ |
(encode-list-of-runs 0 1 (continued-fraction-list-enforce-odd-length! (rat->cf-list rat)))))</syntaxhighlight> |
||
'''The Task''' |
'''The Task''' |
||
< |
<syntaxhighlight lang=scheme>(let ((count 20) |
||
(cw (make-calkin-wilf-gen))) |
(cw (make-calkin-wilf-gen))) |
||
(printf "~%First ~a terms of the Calkin-Wilf sequence:~%" count) |
(printf "~%First ~a terms of the Calkin-Wilf sequence:~%" count) |
||
Line 2,344: | Line 2,344: | ||
(printf "~a @ ~a~%" num (calkin-wilf-position num))) |
(printf "~a @ ~a~%" num (calkin-wilf-position num))) |
||
(let ((num 83116/51639)) |
(let ((num 83116/51639)) |
||
(printf "~a @ ~a~%" num (calkin-wilf-position num)))</ |
(printf "~a @ ~a~%" num (calkin-wilf-position num)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,375: | Line 2,375: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang=ruby>func calkin_wilf(n) is cached { |
||
return 1 if (n == 1) |
return 1 if (n == 1) |
||
1/(2*floor(__FUNC__(n-1)) + 1 - __FUNC__(n-1)) |
1/(2*floor(__FUNC__(n-1)) + 1 - __FUNC__(n-1)) |
||
Line 2,395: | Line 2,395: | ||
with (83116/51639) {|r| |
with (83116/51639) {|r| |
||
say ("\n#{r.as_rat} is at index: ", r2cw(r)) |
say ("\n#{r.as_rat} is at index: ", r2cw(r)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,406: | Line 2,406: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}}s. |
{{trans|Go}}s. |
||
< |
<syntaxhighlight lang=vlang>import math.fractions |
||
import math |
import math |
||
import strconv |
import strconv |
||
Line 2,491: | Line 2,491: | ||
tn := get_term_number(cf) or {0} |
tn := get_term_number(cf) or {0} |
||
println("$r is the ${commatize(tn)}th term of the sequence.") |
println("$r is the ${commatize(tn)}th term of the sequence.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,523: | Line 2,523: | ||
{{libheader|Wren-rat}} |
{{libheader|Wren-rat}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang=ecmascript>import "/rat" for Rat |
||
import "/fmt" for Fmt, Conv |
import "/fmt" for Fmt, Conv |
||
Line 2,572: | Line 2,572: | ||
var cf = toContinued.call(r) |
var cf = toContinued.call(r) |
||
var tn = getTermNumber.call(cf) |
var tn = getTermNumber.call(cf) |
||
Fmt.print("$s is the $,r term of the sequence.", r, tn)</ |
Fmt.print("$s is the $,r term of the sequence.", r, tn)</syntaxhighlight> |
||
{{out}} |
{{out}} |