The '''Calkin-Wilf sequence''' contains every nonnegative rational number exactly once.
It can be calculated recursively as follows:
{{math|a1}} = {{math|1}}
{{math|an+1}} = {{math|1/(2⌊an⌋+1-an)}} for n > 1
;Task part 1:
* Show on this page terms 1 through 20 of the Calkin-Wilf sequence.
To avoid floating point error, you may want to use a rational number data type.
It is also possible, given a non-negative rational number, to determine where it appears in the sequence without calculating the sequence. The procedure is to get the continued fraction representation of the rational and use it as the run-length encoding of the binary representation of the term number, beginning from the end of the continued fraction.
It only works if the number of terms in the continued fraction is odd- use either of the two equivalent representations to achieve this:
{{math|[a0; a1, a2, ..., an]}} = {{math|[a0; a1, a2 ,..., an-1, 1]}}
;Example:
The fraction '''9/4''' has odd continued fraction representation {{math|2; 3, 1}}, giving a binary representation of '''100011''',
which means '''9/4''' appears as the '''35th''' term of the sequence.
;Task part 2:
* Find the position of the number '''83116''''''/''''''51639''' in the Calkin-Wilf sequence.
;Related tasks:
:* [[Fusc sequence]].
;See also:
* Wikipedia entry: [[wp:Calkin%E2%80%93Wilf_tree|Calkin-Wilf tree]]
* [[Continued fraction]]
* [[Continued fraction/Arithmetic/Construct from rational number]]