Find nth Fibonacci number Using Golden ratio (original) (raw)
Last Updated : 8 May, 2026
Given a non-negative integer **n, your task is to find the **nth **Fibonacci **number. The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
The Fibonacci sequence is defined as follows:
- F(0) = 0
- F(1) = 1
- F(n) = F(n - 1) + F(n - 2) for n > 1
**Examples:
**Input: n = 5
**Output: 5
**Explanation: The 5th Fibonacci number is 5.**Input: n = 0
**Output: 0
**Explanation: The 0th Fibonacci number is 0.
Using Golden Ratio
The Fibonacci sequence can be approximated using the golden ratio: f_n = \mathrm{round}(f_{n-1} \times \phi) where, \phi \approx 1.618 .Since, the ratio of consecutive Fibonacci numbers approaches φ, we can use this property to compute the next number.
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). Hence, this method is reliable only from the 5th term onward. After:
f6 = \mathrm{round}(f_5 \times \varphi) = 8
f7 = \mathrm{round}(f_6 \times \varphi) = 13
f8 = \mathrm{round}(f_7 \times \varphi) = 21
f9 = \mathrm{round}(f_8 \times \varphi) = 34
**Note:
- This method can calculate first 34 fibonacci numbers correctly. After that there may be difference from the correct value due to floating point arithmetic errors.
- We can optimize above solution work in O(Log n) by using Efficient Method to Compute Power. C++ `
#include <bits/stdc++.h> using namespace std;
// function finding fibonacci number using golden ratio int nthFibonacci(int n) { // Approximate value of golden ratio double PHI = 1.6180339;
// Fibonacci numbers upto n = 5
int f[6] = {0, 1, 1, 2, 3, 5};
// 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 = 5;
int ans = nthFibonacci(n);
cout << "Fibonacci Number: " << ans << endl;
return 0;}
C
#include <stdio.h> #include <math.h>
// function finding fibonacci number using golden ratio int nthFibonacci(int n) { // Approximate value of golden ratio double PHI = 1.6180339;
// Fibonacci numbers upto n = 5
int f[6] = {0, 1, 1, 2, 3, 5};
// 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 = (int)round(fn * PHI);
t++;
}
return fn;}
// driver code int main() { int n = 5;
int ans = nthFibonacci(n);
printf("Fibonacci Number: %d\n", ans);
return 0;}
Java
import java.util.*;
// solution class class GfG {
// function finding fibonacci number using golden ratio
public static int nthFibonacci(int n)
{
// Approximate value of golden ratio
double PHI = 1.6180339;
// Fibonacci numbers upto n = 5
int[] f = {0, 1, 1, 2, 3, 5};
// 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 = (int)Math.round(fn * PHI);
t++;
}
return fn;
}
// driver code
public static void main(String[] args) {
int n = 5;
int ans = nthFibonacci(n);
System.out.println("Fibonacci Number: " + ans);
}}
Python
import math
function finding fibonacci number using golden ratio
def nthFibonacci(n):
# Approximate value of golden ratio
PHI = 1.6180339
# Fibonacci numbers upto n = 5
f = [0, 1, 1, 2, 3, 5]
# Fibonacci numbers for n < 6
if n < 6:
return f[n]
# Else start counting from 5th term
t, fn = 5, 5
while t < n:
fn = round(fn * PHI)
t += 1
return fndriver code
if name == "main": n = 5
ans = nthFibonacci(n)
print("Fibonacci Number:", ans)C#
using System; using System.Collections.Generic;
// solution class class GfG { // function finding fibonacci number using golden ratio public static int nthFibonacci(int n) { // Approximate value of golden ratio double PHI = 1.6180339;
// Fibonacci numbers upto n = 5
int[] f = {0, 1, 1, 2, 3, 5};
// 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 = (int)Math.Round(fn * PHI);
t++;
}
return fn;
}
// driver code
public static void Main()
{
int n = 5;
int ans = nthFibonacci(n);
Console.WriteLine("Fibonacci Number: " + ans);
}}
JavaScript
// function finding fibonacci number using golden ratio function nthFibonacci(n) { // Approximate value of golden ratio let PHI = 1.6180339;
// Fibonacci numbers upto n = 5
let f = [0, 1, 1, 2, 3, 5];
// 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 = 5;
let ans = nthFibonacci(n);
console.log("Fibonacci Number:", ans);
`
Output
Fibonacci Number: 5
**Time complexity: O(n)
**Auxiliary space: O(1)