Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding next fibonacci number

Tags:

php

fibonacci

I need to find a (the next) fibonacci number given a integer N. So let's say I have n = 13 and I need to output the next fibonacci number which is 21 but how do I do this? How can I find the previous number that summed up to form it?

I mean I could easily come up with a for/while loop that returns the fibonacci sequence but how can I find the next number by being given the previous one.

<?php

$n = 13;

while($n < 1000) {

    $n = $x + $y; 
    echo($n."<br />"); 
    $x = $y;
    $y = $n;
}
?>
like image 355
Mike Fonder Avatar asked Jun 17 '14 15:06

Mike Fonder


People also ask

How do you find the next Fibonacci sequence?

The Fibonacci sequence is defined by , for all , when and . In other words, to get the next term in the sequence, add the two previous terms. The notation that we will use to represent the Fibonacci sequence is as follows: f1=1,f2=1,f3=2,f4=3,f5=5,f6=8,f7=13,f8=21,f9=34,f10=55,f11=89,f12=144,…

How do you find the nearest Fibonacci number to a given number?

The closest Fibonacci number is defined as the Fibonacci number with the smallest absolute difference with the given integer. For example, 34 is the closest Fibonacci number to 30 , because |34 - 30| = 4 , which is smaller than the second closest one, 21 , for which |21 - 30| = 9 .

What is the next Fibonacci number in the Fibonacci sequence 0 1 1 2 3 5 Why?

Solution: The Fibonacci series is the series of numbers 1, 1, 2, 3, 5, 8, 13, 21, ... Therefore, the next Fibonacci number in the following sequence is 34.

How do you find the next Fibonacci number in Python?

def Fibonacci(n): if n<0: print("Incorrect input") elif n==1: return 0 elif n==2: return 1 else: return Fibonacci(n-1)+Fibonacci(n-2) print(Fibonacci(9)) if i entered 9 then the function should return 13 which is next fibonacci number.


3 Answers

You can use Binet's Formula:

          n          -n
F(n) = phi   - (-phi)
       ---------------
          sqrt(5)

where phi is the golden ratio (( 1 + sqrt(5) ) / 2) ~= 1.61803...

This lets you determine exactly the n-th term of the sequence.

like image 85
Marc B Avatar answered Oct 13 '22 14:10

Marc B


Using a loop you could store the values in an array that could stop immediately one key after finding the selected number in the previous keys value.

function getFib($n) {

   $fib = array($n+1);       // array to num + 1
   $fib[0] = 0; $fib[1] = 1; // set initial array keys
   $i;

   for ($i=2;$i<=$n+1;$i++) {
      $fib[$i] = $fib[$i-1]+$fib[$i-2];
        if ($fib[$i] > $n) { // check if key > num 
            return $fib[$i];
            }
        }
    if ($fib[$i-1] < $n) {   // check if key < num
        return $fib[$i-1] + $n;
    }
    if ($fib[$i] = $n-1) {   // check if key = num
        return $fib[$i-1] + $fib[$i-2];
    } 
    if ($fib[$i-1] = 1) {    // check if num = 1
        return $n + $n;
    }
}

$num = 13;
echo "next fibonacci number = " . getFib($num);

Please note that I haven't tested this out and the code could be optimized, so before downvoting consider this serves only as a concept to the question asked.

like image 43
l'L'l Avatar answered Oct 13 '22 14:10

l'L'l


You can do it in 1 step:

phi = (1+sqrt(5))/2

next = round(current*phi)

(Where round is a function that returns the closest integer; basically equivalent to floor(x+0.5))

For example, if your current number is 13: 13 * phi = 21.034441853748632, which rounds to 21.

like image 39
12Me21 Avatar answered Oct 13 '22 13:10

12Me21