Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fibonacci Recursion using Golden Ratio(Golden Number)

i can do a recursive function to compute the nth Fibonacci term as follows:

int rec (int i)
{
  if(i == 1 || i == 2)
    return i;
else return rec(i-1)+rec(i-2);

}

But i want to use the golden number which is 1.618 to compute the Fibonacci; but my attempt fails, i get wrong numbers:

int rec (int i)
{
  if(i == 1 || i ==  2)
    return i;

  else return 1.618*rec(i-1);

 }

How can i get it to work?

like image 378
Marwan Tushyeh Avatar asked Dec 26 '22 08:12

Marwan Tushyeh


1 Answers

The golden ratio is an irrational number, so you shouldn't necessarily expect to be able to plug an approximation of it into a formula to get an exact result.

If you want to know how to calculate the nth fibonacci number quickly, here is a page that lists a variety of methods in decreasing order of runtime (but increasing in order of implementation difficulty): https://www.nayuki.io/page/fast-fibonacci-algorithms

like image 76
Cam Avatar answered Dec 28 '22 23:12

Cam