斐波那契级数=0,1,1,2,3,5,8,13,21,34……。。 求n个斐波那契数的不同方法 已经讨论过了。另一种求第n个斐波那契数的简单方法是使用黄金分割比,因为斐波那契数保持近似黄金分割比直到无穷大。 黄金比例: 例如:
null
Input : n = 9Output : 34Input : n = 7Output : 13
方法: 黄金比例可能会给我们 回答不正确。 如果我们把每个点的结果加起来,就可以得到正确的结果。
nth fibonacci number = round(n-1th Fibonacci number X golden ratio) fn = round(fn-1 *)
直到第四学期,这一比例还不太接近黄金比例(3/2=1.5,2/1=2,…)。因此,我们将考虑从第五个学期获得下一个斐波那契数。要找出第九个斐波那契数f9(n=9):
f6 = round(f5 *) = 8f7 = round(f6 *
) = 13f8 = round(f7 *
) = 21f9 = round(f8 *
) = 34
注: 这种方法可以正确计算前34个斐波那契数。之后可能会出现与正确值的差异。 以下是上述方法的实施情况:
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 ), "" ; // 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> |
输出:
9th Fibonacci Number = 34
我们可以使用 计算功率的有效方法 . 由于涉及浮点计算,上述方法可能并不总能产生正确的结果。这就是为什么,这种方法没有被实际使用,即使它可以优化为在O(logn)中工作。请参考以下麻省理工学院视频了解更多细节。 https://www.youtube.com/watch?v=-EQTVuAhSFY
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END