Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions math/nth_fibonacci_using_goldenratio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// CPP program to find n-th Fibonacci number
// More documentation about the algorithm
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/
// https://github.com/allalgorithms/cpp
//
// Contributed by: Mohbius
// Github: @mohbius
//
#include <iostream>
#include <cstdlib>
#include <cmath>
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;
}

int main()
{
std::cout << fib(9) << std::endl; // 34
std::cout << fib(8) << std::endl; // 21
std::cout << fib(7) << std::endl; // 13
return 0;
}