6

I've got an unusual (I think) problem. For a given number F_n (I don't know the value of n), I have to find numbers F_0, F_1 such that F_{n}=F_{n-1}+F_{n-2}. The additional difficulty is that this sequence should be as long as possible (value n for F_n should be the highest) and if there exist more then one solution I must take this with the smallest F_0. In short I must generate my "own" Fibonacci sequence. Some examples:

in: F_n = 10; out: F_0 = 0; F_1 = 2;

in: F_n = 17; out: F_0 = 1; F_1 = 5;

in: F_n = 4181; out: F_0 = 0; F_1 = 1;

What I observed for every sequence (with "Fibonacci rule") F_n there is:

F_n = Fib_n * F_1 + Fib_{n-1} * F_0

Where Fib_n is the n-th Fibonacci number. It is true especially for Fibonacci sequence. But I do not know whether this observation is worth anything. We don't know n and our task is to find F_1, F_0 so I think we have gained nothing. Any ideas?

xan
  • 1,053
  • 1
  • 10
  • 29
  • How about `F_1 = F_n` and `F_0 = 0`? You get the smallest possible `F_0`! – Shahbaz Apr 14 '12 at 14:33
  • @Shahbaz: but not the longest sequence. – Gareth Rees Apr 14 '12 at 14:34
  • Do F_0 and F_1 have to be nonnegative? – oldboy Apr 14 '12 at 14:37
  • @oldboy, yes, I should have written this – xan Apr 14 '12 at 14:39
  • (4,1,5,6,11,17) is longer than (1,5,6,11,17) ;) – n. m. could be an AI Apr 14 '12 at 15:23
  • so we got another condition: F_{n} >= F_{n-1} :-) – xan Apr 14 '12 at 16:08
  • And how about finding F_{n-1} for a given F_n ? My friend told me that I should look at it this way.. Let F_n = 17 as in the example above, then when we know that F_{n-1} = 11 we have: F_{n-2} = 17 - 11 = 6; F_{n-3} = 11 - 6 = 5; F_{n-4} = 6 - 5 = 1 and we found first two terms (terms have to be nonnegative): F_0=1 and F_1=5. But the problem is how to find F_{n-1}, anyone has an idea? Maybe it's the easiest way to solve this problem. – xan Apr 16 '12 at 16:33

5 Answers5

3

Fn-1 = round(Fn/φ)

where φ=(√5+1)/2.

Proof is left as an exercise for the reader ;^P

Update This is incorrect, back to the drawing board.

Update 2 Let's compute backwards from Fn and Fn-1.

Fn-2 = Fn - Fn-1
Fn-3 = Fn-1 - Fn-2 = Fn-1 - (Fn - Fn-1) = 2Fn-1 - Fn
Fn-4 = Fn-2 - Fn-3 = (Fn - Fn-1) - (2Fn-1 - Fn) = 2Fn - 3Fn-1
Fn-5 = Fn-3 - Fn-4 = (2Fn-1 - Fn) - (2Fn - 3Fn-1) = 5Fn-1 - 3Fn
Fn-6 = Fn-4 - Fn-5 = (2Fn - 3Fn-1) - (5Fn-1 - 3Fn) = 5Fn - 8Fn-1

Notice the pattern? It is easy to compute any member of the sequence out of the real Fibonacci sequence and the last two members. But we only know the last member, how can we know the one before last?

Let's write down the requirement Fi>0 in terms of Fn-1.

Fn-2 = Fn - Fn-1 > 0 ⇒ Fn-1 < Fn
Fn-3 = 2Fn-1 - Fn > 0 ⇒ Fn-1 > Fn/2
Fn-4 = 2Fn - 3Fn-1 ⇒ Fn-1 < 2Fn/3
Fn-5 = 5Fn-1 - 3Fn ⇒ Fn-1 > 3Fn/5

So we have a sequence of bounds on Fn-1 in written terms of the real Fibonacci sequence, each one tighter than the previous. The very last bound that is still satisfiable determines Fn-1 that corresponds to the longest sequence. If there's more than one number that satisfies the last bound, use either the smallest or the largest one , depending on whether the sequence has even or odd length.

For instance, if Fn=101, then
101*5/8 < Fn-1 < 101*8/13 ⇒ Fn-1 = 63

The previous (incorrect) solution would imply Fn-1 = 62, which is close but no cigar.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • @Karoly Horvath: this is not the longest Fibonacci-like sequence that ends in 101. – n. m. could be an AI Apr 14 '12 at 17:05
  • 2
    *Proof is left as an exercise for the reader* Don't say this if you're just guessing, and you are, because [11, 1, 12, 13, 25, 38, 63, 101] is better than [9, 7, 16, 23, 39, 62, 101]. It creates a never-ending headache for those of us knowledgeable enough to use it responsibly. – oldboy Apr 14 '12 at 17:05
  • @oldboy You are right, I thoight I have a proof but apparently it's full of holes. – n. m. could be an AI Apr 14 '12 at 17:16
  • It is on the other hand a very good approximation - I tested all the numbers from 10 to 1000 and it was never more than 1.5 away from the best second last element. – Neil Apr 14 '12 at 23:31
  • It turns out that this formula: F_{n-1} = round(F_n/φ) is absolutely correct. Every sequence with 'Fibonacci rule' that is F_n=F_{n-1}+F_{n-2} satisfy: lim n to infty F_{n+1}/F_n = φ. This follows directly from the generating functions of these sequences. – xan Apr 17 '12 at 16:54
  • or maybe it is not that simple for small numbers (I'm not sure how to prove that) but in general: it is what author of this task had in mind, it's ok.. – xan Apr 17 '12 at 17:00
2

I'm not sure what you are looking for. The recursive series you mention is defined as:

Fn = F{n-1} + F{n-2}

Clearly, if the staring values can be anything, we can arbirarlily choose F{n-1}, which will give F{n-2} ( = Fn=F{n-1}). Knowing that F{n-1} = F{n-2} + F{n-3}, follows that F{n-3} can be computed from F{n-1} and F{n-2}. This means that you can trace the numbers back to infinity, thus there is no "longest" sequence and there are infinitely many valid sequences.

In a way you are calculating an inverse Fibonacci sequence with initial values F{n} and F{n-1} where F{i} = F{i+2}-F{i+1}, i forever decreasing

UPDATE: If you are looking to constrain the solutionspace to results where all F{i} are non-negative integers, you will get only a handful (most of the time singleton) solutions.

If you calculate the original Fibonacci numbers (Fib{i}), soon you get F{n} < Fib{i-1}; clearly you do not need to go any further. Then you can try all possible combinations of F{0} and F{1} such that F{n} <= Fib{i} * F{1}+ Fib{i-1} * F{0} -- there are only a finite amount of possibilities for non-negative F{0} and F{1} (you can obviously rule out F{0}=F{1}=0). Then see which i(s) is(are) highest amongst the ones that satisfy equality -- you get F{0} and F{1} as well

Attila
  • 28,265
  • 3
  • 46
  • 55
  • I'm terribly sorry, I simply did not sleep too well and didn't wrote strctly.. "The longest sequence" I mean the sequence end at given F_n. Simply, value n (which we do not know, I introduced it for better understanding) must be the highest. – xan Apr 14 '12 at 14:37
  • 1
    altough he didn't mention it I'm sure he is looking for *non-negative* *integers* – Karoly Horvath Apr 14 '12 at 14:38
  • Yes, so you go "backwards" from `n`, and you find there is no "highest" `n`, such that the sequence stop. – Attila Apr 14 '12 at 14:38
  • It's an easy guess that this would be defined over the whole numbers, so 0 < F(0) <= F(1). And by adding the constraint that the sequence be as long as possible, the difference between F(0) and F(1) should be minimized, subject to the final result coming out. And F(0) should be as small as possible. Given all that there is a longest sequence (possibly more than one); and there are not infinitely many valid sequences. – DRVic Apr 14 '12 at 14:39
  • @DRVic, exactly.. And if there exist more than one sequence we must take this with the smallest F_0. F_n are non-negative integers.. – xan Apr 14 '12 at 14:45
  • @all - I guessed as much, but the wording of the question allowed my reasoning to be true. See updated answer for the additional constraints – Attila Apr 14 '12 at 14:51
2

Your equation

F_n = Fib_n * F_1 + Fib_{n-1} * F_0

is a linear Diophantine equation in two variables F_1 and F_0. The link presents an efficient algorithm to compute a description of the solution set that allows you to find a solution, if one exists, with F_1 >= 0 and F_0 >= 0 and F_0 minimum. You can then attempt to guess n = 0, 1, ... until you find that there's no solution. This approach is polynomial in log(F_n).

oldboy
  • 514
  • 3
  • 4
  • Bonus: you can use [Cassini's identity](http://en.wikipedia.org/wiki/Cassini%27s_identity) to avoid implementing extended Euclidean GCD. – oldboy Apr 14 '12 at 14:54
  • It's very hard to code, but I think the best approach, at least I like it the most. – xan Apr 14 '12 at 22:40
2

F_n = Fib_n * F_1 + Fib_{n-1} * F_0

Where Fib_n is the n-th Fibonacci number. It is true especially for Fibonacci sequence. But I do not know whether this observation is worth anything. We don't know n and our task is to find F_1, F_0 so I think we have gained nothing.

Well, you're looking for the longest sequence, this means you want to maximize n.

Create a lookup table for fibonacci numbers. Start with the biggest n such that Fib_n <= F_n, the previous entry in the table is fib_n{n-1}. Now the only variables are F_1 and F_0. Solve the linear Diophantine equation. If it has a solution, you finished. If not, decrease n by 1 and try again, till you find a solution.

Note: a solution always exists, since F_2 = 1 * F_1 + 0 * F_0 has the solution F_2 = F_1.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • ((Shahbaz quipped the solution from the note 21 mins before this post - which doesn't mean (s)he(never mind the facial hair) observed it first.) If only you managed to allow for _F_1 < F_0_ … – greybeard Oct 07 '16 at 06:23
0

Mimic the computation of Fibonacci numbers with a matrix (but with different initial values). [[0 1] [1 1]] to power k will yield [F_{n+k}, F_{n+1+k}] when multiplied by [F_{n}, F_{n+1}].

Since raising of a matrix to a power is an O(log n) (assuming multiplications of integers is O(1)), you can perform the whole calculation in O(log n) time until matrix coefficients start to dominate the computation.

I don't know how large the numbers with which you are working happen to be, but the nth power of this matrix is [[F_{n-1} F_n] [F_n F_{n+1}]]. And log(F_n) ~ n/5 (so the billionth Fibonacci number will have roughly 200 million digits).

Dmitry Rubanovich
  • 2,471
  • 19
  • 27