Talk:Calkin-Wilf sequence

From Rosetta Code
Revision as of 05:25, 29 December 2020 by rosettacode>Paddy3118 (→‎Off by one error?: Index starts from one.)

Off by one error?

The wikipedia entry starts the series with 1 not zero. The calculation of what term represents a rational also seems off by one. --Paddy3118 (talk) 22:29, 28 December 2020 (UTC)

Other calculations fail as 0 is never a term if calculating the i'th term from the run length encodings of i for example. Best to correct the task wording and adjust all examples I think. --Paddy3118 (talk) 23:37, 28 December 2020 (UTC)

The 123456789'th term of 83116 / 51639 applies to the wikipedia series where the "first" term is 1.

I get, using the wikipedia calcs:

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(i, ith_term(i))
0 1
1 1
2 1/2
3 2
4 1/3
5 3/2
6 2/3
7 3
8 1/4
9 4/3

zeroth, and first terms are both 1. Best to do as wikepedia does and have istart at 1 for the series I think? --Paddy3118 (talk) 00:07, 29 December 2020 (UTC)

When I try this, I get 'error, ith_term has not been defined'. To be slightly less churlish, I see wp defines "starting from q1 = 1" and there simply is no "zeroth" term. A q0 of 1 is just as wrong, in fact even wronger, and I'd like to see that ith_term - if it is using the formula I see on wp and in the task description it would a) be wrong and b) not be possible without assuming a q0 of 0. In my entry I cheekily went printf(1,"The first 21 terms of the Calkin-Wilf sequence are:\n 0: 0\n") just to match everyone else. Perhaps the task could be amended to say "you can quietly assume a q0 of 0 to simplify calculations but do not show it". Lastly, when you say "seems off by one" the wikipedia page clearly links 4/3 and 9 and 3/4 and 14 so... --Pete Lomax (talk) 00:37, 29 December 2020 (UTC)
I read the wp entry some more as well as others, and I agree, there is no zero'th indexed item. The series starts from the 1-indexed item which has a value of 1. Different methods of arriving at the i'th term, for i being one of all positive integers not including zero, agree. Extrapolating to a zero'th term do not, and have no meaning in terms of the tree that is traversed to form the series.
I could amend the task description... --Paddy3118 (talk) 05:24, 29 December 2020 (UTC)