You get an integer n and you need to find the index of its first appearance in Stern's Diatomic Sequence.
The sequence is defined like this:
a[0] = 0
a[1] = 1
a[2*i] = a[i]
a[2*i+1] = a[i] + a[i+1]
See MathWorld.
Because n can be up to 400000, it's not a good idea to brute-force it, especially since the time limit is 4000 ms.
The sequence is pretty odd: first occurrence of 8 is 21, but first occurrence of 6 is 33.
Any ideas how to solve this?
Maybe this might help: OEIS
We can easily solve for the first occurrence of a number in the range of 400000 in under four seconds:
The key to it is the Calkin-Wilf tree.
Starting from the fraction
1/1, it is built by the rule that for a node with the fractiona/b, its left child carries the fractiona/(a+b), and its right child the fraction(a+b)/b.etc. The diatomic sequence (starting at index 1) is the sequence of numerators of the fractions in the Calkin-Wilf tree, when that is traversed level by level, each level from left to right.
If we look at the tree of indices
we can easily verify that the node at index
kin the Calkin-Wilf tree carries the fractiona[k]/a[k+1]by induction.That is obviously true for
k = 1(a[1] = a[2] = 1), and from then on,for
k = 2*jwe have the left child of the node with indexj, so the fraction isa[j]/(a[j]+a[j+1])anda[k] = a[j]anda[k+1] = a[j] + a[j+1]are the defining equations of the sequence.for
k = 2*j+1we have the right child of the node with indexj, so the fraction is(a[j]+a[j+1])/a[j+1]and that isa[k]/a[k+1]again by the defining equations.All positive reduced fractions occur exactly once in the Calkin-Wilf tree (left as an exercise for the reader), hence all positive integers occur in the diatomic sequence.
We can find the node in the Calkin-Wilf tree from the index by following the binary representation of the index, from the most significant bit to the least, for a 1-bit we go to the right child and for a 0-bit to the left. (For that, it is nice to augment the Calkin-Wilf tree with a node
0/1whose right child is the1/1node, so that we need have a step for the most significant set bit of the index.)Now, that doesn't yet help very much to solve the problem at hand.
But, let us first solve a related problem: For a reduced fraction
p/q, determine its index.Suppose that
p > q. Then we know thatp/qis a right child, and its parent is(p-q)/q. If alsop-q > q, we have again a right child, whose parent is(p - 2*q)/q. Continuing, ifthen we reach the
p/qnode from theb/qnode by going to the right childatimes.Now we need to find a node whose numerator is smaller than its denominator. That is of course the left child of its parent. The parent of
b/qisb/(q-b)then. Ifwe have to go to the left child
ctimes from the nodeb/dto reachb/q.And so on.
We can find the way from the root (
1/1) to thep/qnode using the continued fraction (I consider only simple continued fractions here) expansion ofp/q. Letp > qandthe continued fraction expansion of
p/qending in1.ris even, then go to the right childa_rtimes, then to the lefta_(r-1)times, then to the right child ... thena_1times to the left child, and finallya_0times to the right.ris odd, then first go to the left childa_rtimes, thena_(r-1)times to the right ... thena_1times to the left child, and finallya_0times to the right.For
p < q, we must end going to the left, hence start going to the left for evenrand start going to the right for oddr.We have thus found a close connection between the binary representation of the index and the continued fraction expansion of the fraction carried by the node via the path from the root to the node.
Let the run-length-encoding of the index
kbei.e. the binary representation of
kstarts withc_1ones, followed byc_2zeros, thenc_3ones etc., and ending withc_jkis odd - hencejis also odd;kis even - hencejis also even.Then
[c_j, c_(j-1), ..., c_2, c_1]is the continued fraction expansion ofa[k]/a[k+1]whose length has the same parity ask(every rational has exactly two continued fraction expansions, one with odd length, the other with even length).The RLE gives the path from the
0/1node above1/1toa[k]/a[k+1]. The length of the path isk, andNow, to find the index of the first occurrence of
n > 0in the diatomic sequence, we first observe that the smallest index must necessarily be odd, sincea[k] = a[k/2]for evenk. Let the smallest index bek = 2*j+1. Thenkis odd,kisa[2*j+1]/a[2*j+2] = (a[j] + a[j+1])/a[j+1], hence it is a right child.So the smallest index
kwitha[k] = ncorresponds to the left-most ending of all the shortest paths to a node with numeratorn.The shortest paths correspond to the continued fraction expansions of
n/m, where0 < m <= nis coprime ton[the fraction must be reduced] with the smallest sum of the partial quotients.What kind of length do we need to expect? Given a continued fraction
p/q = [a_0, a_1, ..., a_r]witha_0 > 0and sumthe numerator
pis bounded byF(s+1)and the denominatorqbyF(s), whereF(j)is thej-th Fibonacci number. The bounds are sharp, fora_0 = a_1 = ... = a_r = 1the fraction isF(s+1)/F(s).So if
F(t) < n <= F(t+1), the sum of the partial quotients of the continued fraction expansion (either of the two) is>= t. Often there is anmsuch that the sum of the partial quotients of the continued fraction expansion ofn/mis exactlyt, but not always:and the continued fraction expansions of the two reduced fractions
6/mwith0 < m <= 6arewith sum of the partial quotients 6. However, the smallest possible sum of partial quotients is never much larger (the largest I'm aware of is
t+2).The continued fraction expansions of
n/mandn/(n-m)are closely related. Let's assume thatm < n/2, and letThen
a_0 >= 2,and since
the continued fraction expansion of
n/(n-m)isIn particular, the sum of the partial quotients is the same for both.
Unfortunately, I'm not aware of a way to find the
mwith the smallest sum of partial quotients without brute force, so the algorithm is (I assumen > 2for
0 < m < n/2coprime ton, find the continued fraction expansion ofn/m, collecting the ones with the smallest sum of the partial quotients (the usual algorithm produces expansions whose last partial quotient is> 1, we assume that).Adjust the found continued fraction expansions [those are not large in number] it the following way:
[a_0, a_1, ..., a_r]has even length, convert it to[a_0, a_1, ..., a_(r-1), a_r - 1, 1][1, a_0 - 1, a_1, ..., a_(r-1), a_r - 1, 1](that chooses the one between
n/mandn/(n-m)leading to the smaller index)reverse the continued fractions to obtain the run-length-encodings of the corresponding indices
choose the smallest among them.
In step 1, it is useful to use the smallest sum found so far to short-cut.
Code (Haskell, since that's easiest):