Skip to content
Related Articles

Related Articles

Improve Article

Find nth Fibonacci number using Golden ratio

  • Difficulty Level : Easy
  • Last Updated : 25 Feb, 2021
Geek Week

Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ……..
Different methods to find nth Fibonacci number are already discussed. Another simple way of finding nth Fibonacci number is using golden ratio as Fibonacci numbers maintain approximate golden ratio till infinite. 
Golden ratio: 
\varphi ={\frac {1+{\sqrt {5}}}{2}}=1.6180339887\ldots
Examples: 
 

Input : n = 9
Output : 34

Input : n = 7
Output : 13

 

Approach: 
Golden ratio may give us incorrect answer. 
We can get correct result if we round up the result at each point. 
 

nth fibonacci number = round(n-1th Fibonacci number X golden ratio)
                  fn = round(fn-1 * \varphi)

Till 4th term, the ratio is not much close to golden ratio (as 3/2 = 1.5, 2/1 = 2, …). So, we will consider from 5th term to get next fibonacci number. To find out the 9th fibonacci number f9 (n = 9) : 
 

     f6 = round(f5 * \varphi) = 8
f7 = round(f6 * \varphi) = 13
f8 = round(f7 * \varphi) = 21
f9 = round(f8 * \varphi) = 34

Note: This method can calculate first 34 fibonacci numbers correctly. After that there may be difference from the correct value. 
Below is the implementation of above approach: 
 



CPP




// CPP program to find n-th Fibonacci number
#include <bits/stdc++.h>
using namespace std;
 
// Approximate value of golden ratio
double PHI = 1.6180339;
 
// Fibonacci numbers upto n = 5
int f[6] = { 0, 1, 1, 2, 3, 5 };
 
// Function to find nth
// Fibonacci number
int fib (int n)
{
    // Fibonacci numbers for n < 6
    if (n < 6)
        return f[n];
 
    // Else start counting from
    // 5th term
    int t = 5, fn = 5;
 
    while (t < n) {
         fn = round(fn * PHI);
         t++;
    }
 
    return fn;  
}
 
// driver code
int main()
{
    int n = 9;
 
    cout << n << "th Fibonacci Number = "
         << fib(n) << endl;
 
    return 0;
}

Java




// Java program to find n-th Fibonacci number
 
class GFG
{
    // Approximate value of golden ratio
    static double PHI = 1.6180339;
     
    // Fibonacci numbers upto n = 5
    static int f[] = { 0, 1, 1, 2, 3, 5 };
     
    // Function to find nth
    // Fibonacci number
    static int fib (int n)
    {
        // Fibonacci numbers for n < 6
        if (n < 6)
            return f[n];
     
        // Else start counting from
        // 5th term
        int t = 5;
        int fn = 5;
     
        while (t < n) {
            fn = (int)Math.round(fn * PHI);
            t++;
        }
     
        return fn;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 9;
        System.out.println(n + "th Fibonacci Number = "
                                                +fib(n));
    }
}
 
// This code is contributed by Anant Agarwal.

Python3




# Python3 code to find n-th Fibonacci number
 
# Approximate value of golden ratio
PHI = 1.6180339
 
# Fibonacci numbers upto n = 5
f = [ 0, 1, 1, 2, 3, 5 ]
 
# Function to find nth
# Fibonacci number
def fib ( n ):
 
    # Fibonacci numbers for n < 6
    if n < 6:
        return f[n]
 
    # Else start counting from
    # 5th term
    t = 5
    fn = 5
     
    while t < n:
        fn = round(fn * PHI)
        t+=1
     
    return fn
 
# driver code
n = 9
print(n, "th Fibonacci Number =", fib(n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#




// C# program to find n-th Fibonacci
// number
using System;
 
class GFG {
     
    // Approximate value of golden ratio
    static double PHI = 1.6180339;
     
    // Fibonacci numbers upto n = 5
    static int []f = { 0, 1, 1, 2, 3, 5 };
     
    // Function to find nth
    // Fibonacci number
    static int fib (int n)
    {
         
        // Fibonacci numbers for n < 6
        if (n < 6)
            return f[n];
     
        // Else start counting from
        // 5th term
        int t = 5;
        int fn = 5;
     
        while (t < n) {
            fn = (int)Math.Round(fn * PHI);
            t++;
        }
     
        return fn;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 9;
         
        Console.WriteLine(n + "th Fibonacci"
                    + " Number = " + fib(n));
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to find n-th
// Fibonacci number Approximate
// value of golden ratio
$PHI = 1.6180339;
 
// Fibonacci numbers
// upto n = 5
 
// Function to find nth
// Fibonacci number
function fib ($n)
{
    global $PHI;
    $f = array(0, 1, 1, 2, 3, 5);
     
    // Fibonacci numbers
    // for n < 6
    if ($n < 6)
        return $f[$n];
 
    // Else start counting
    // from 5th term
    $t = 5;
    $fn = 5;
 
    while ($t < $n)
    {
        $fn = round($fn * $PHI);
        $t++;
    }
 
    return $fn;
}
 
    // Driver Code
    $n = 9;
    echo $n, "th Fibonacci Number = ",
        fib($n), "\n";
 
// This code is contributed by aj_36
?>

Javascript




<script>
 
// JavaScript program to find n-th Fibonacci number
 
  // Approximate value of golden ratio
    let PHI = 1.6180339;
   
    // Fibonacci numbers upto n = 5
    let f = [ 0, 1, 1, 2, 3, 5 ];
   
    // Function to find nth
    // Fibonacci number
    function fib (n)
    {
        // Fibonacci numbers for n < 6
        if (n < 6)
            return f[n];
   
        // Else start counting from 
        // 5th term
        let t = 5, fn = 5;
   
        while (t < n) {
             fn = Math.round(fn * PHI);
             t++;
        }
   
        return fn;   
    }
   
    // driver code
 
    let n = 9;
     document.write(n + "th Fibonacci Number = " + fib(n) + "<br>");
   
// This code is contributed by Mayank Tyagi
 
</script>

Output: 
 

9th Fibonacci Number = 34

We can optimize above solution work in O(Log n) by using efficient method to compute power.
The above method may not always produce correct results as floating point computations are involved. This is the reason, this method is not used practically even if it can be optimized to work in O(Log n). Please refer below MIT video for more details.
https://www.youtube.com/watch?v=-EQTVuAhSFY
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :