📜  查找下一个斐波那契数

📅  最后修改于: 2021-04-27 06:13:52             🧑  作者: Mango

给定斐波那契数N ,任务是找到下一个斐波那契数。
例子:

方法:斐波那契数列中两个相邻数字的比值迅速接近((1 + sqrt(5))/ 2) 。因此,如果将N乘以(((1 + sqrt(5))/ 2)并将其四舍五入,则结果数将是下一个斐波那契数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// Function to return the next
// fibonacci number
int nextFibonacci(int n)
{
    double a = n * (1 + sqrt(5)) / 2.0;
    return round(a);
}
 
// Driver code
int main()
{
    int n = 5;
    cout << nextFibonacci(n);
}
 
// This code is contributed by mohit kumar 29


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the next
    // fibonacci number
    static long nextFibonacci(int n)
    {
        double a = n * (1 + Math.sqrt(5)) / 2.0;
        return Math.round(a);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 5;
        System.out.println(nextFibonacci(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
from math import *
 
# Function to return the next
# fibonacci number
def nextFibonacci(n):
    a = n*(1 + sqrt(5))/2.0
    return round(a)
 
# Driver code
n = 5
print(nextFibonacci(n))


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the next
    // fibonacci number
    static long nextFibonacci(int n)
    {
        double a = n * (1 + Math.Sqrt(5)) / 2.0;
        return (long)Math.Round(a);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5;
        Console.WriteLine(nextFibonacci(n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
8