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
I would recommend you read this letter from Dijkstra which explains an alternative way of computing this function via:
This starts with a,b=1,0 and effectively uses successive bits of N (from least to most significant) to increase a and b, the final result being the value of b.
The index of the first appearance of a particular value for b can therefore be computed via finding the smallest n for which this iteration will result in that value of b.
One method for finding this smallest n is to use A* search where the cost is the value of n. The efficiency of the algorithm will be determined by your choice of heuristic.
For the heuristic, I would recommend noting that:
EDIT
Here is some example Python code to illustrate the A* approach.
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
k
in 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*j
we 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+1
we 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/1
whose right child is the1/1
node, 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/q
is 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/q
node from theb/q
node by going to the right childa
times.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/q
isb/(q-b)
then. Ifwe have to go to the left child
c
times from the nodeb/d
to reachb/q
.And so on.
We can find the way from the root (
1/1
) to thep/q
node using the continued fraction (I consider only simple continued fractions here) expansion ofp/q
. Letp > q
andthe continued fraction expansion of
p/q
ending in1
.r
is even, then go to the right childa_r
times, then to the lefta_(r-1)
times, then to the right child ... thena_1
times to the left child, and finallya_0
times to the right.r
is odd, then first go to the left childa_r
times, thena_(r-1)
times to the right ... thena_1
times to the left child, and finallya_0
times to the right.For
p < q
, we must end going to the left, hence start going to the left for evenr
and 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
k
bei.e. the binary representation of
k
starts withc_1
ones, followed byc_2
zeros, thenc_3
ones etc., and ending withc_j
k
is odd - hencej
is also odd;k
is even - hencej
is 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/1
node above1/1
toa[k]/a[k+1]
. The length of the path isk
, andNow, to find the index of the first occurrence of
n > 0
in 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
. Thenk
is odd,k
isa[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
k
witha[k] = n
corresponds 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 <= n
is 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 > 0
and sumthe numerator
p
is bounded byF(s+1)
and the denominatorq
byF(s)
, whereF(j)
is thej
-th Fibonacci number. The bounds are sharp, fora_0 = a_1 = ... = a_r = 1
the 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 anm
such that the sum of the partial quotients of the continued fraction expansion ofn/m
is exactlyt
, but not always:and the continued fraction expansions of the two reduced fractions
6/m
with0 < m <= 6
arewith 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/m
andn/(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
m
with the smallest sum of partial quotients without brute force, so the algorithm is (I assumen > 2
for
0 < m < n/2
coprime 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/m
andn/(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):