📜  计算O(Log n)时间和O(1)空间中给定范围内的斐波那契数

📅  最后修改于: 2021-04-26 09:31:39             🧑  作者: Mango

给定范围,计算给定范围内的斐波那契数。前几个斐波那契数是0、1、1、2、3、5、8、13、21、34、55、89、141 、.
例子 :

Input: low = 10, high = 100
Output: 5
There are five Fibonacci numbers in given
range, the numbers are 13, 21, 34, 55 and 89.

Input: low = 10, high = 20
Output: 1
There is only one Fibonacci Number, 13.

Input: low = 0, high = 1
Output: 3
Fibonacci numbers are 0, 1 and 1

强烈建议您最小化浏览器,然后自己尝试。
蛮力解决方案是一个一个地找到所有斐波那契数,并在给定范围内计算所有斐波那契数
一个有效的解决方案是使用简单的Fibonacci公式使用前一个斐波那契数生成下一个,即f n = f n-1 + f n-2

C++
// C++ program to count Fibonacci numbers in given range
#include 
using namespace std;
 
// Returns count of fibonacci numbers in [low, high]
int countFibs(int low, int high)
{
    // Initialize first three Fibonacci Numbers
    int f1 = 0, f2 = 1, f3 = 1;
 
    // Count fibonacci numbers in given range
    int result = 0;
 
    while (f1 <= high)
    {
        if (f1 >= low)
        result++;
        f1 = f2;
        f2 = f3;
        f3 = f1 + f2;
    }
 
    return result;
}
 
// Driver program
int main()
{
int low = 10, high = 100;
cout << "Count of Fibonacci Numbers is "
        << countFibs(low, high);
return 0;
}


Python3
# Python3 program to count Fibonacci
# numbers in given range
 
# Returns count of fibonacci
# numbers in [low, high]
def countFibs(low, high):
     
    # Initialize first three
    # Fibonacci Numbers
    f1, f2, f3 = 0, 1, 1
 
    # Count fibonacci numbers in
    # given range
    result = 0
 
    while (f1 <= high):
        if (f1 >= low):
            result += 1
        f1 = f2
        f2 = f3
        f3 = f1 + f2
 
    return result
 
# Driver Code
low, high = 10, 100
print("Count of Fibonacci Numbers is",
                 countFibs(low, high))
 
# This code is contributed
# by mohit kumar


C#
// C# program to count Fibonacci
// numbers in given range
using System;
 
public class GFG
{
     
    // Returns count of fibonacci
    // numbers in [low, high]
    static int countFibs(int low,
                        int high)
    {
         
        // Initialize first three
        // Fibonacci Numbers
        int f1 = 0, f2 = 1, f3 = 1;
     
        // Count fibonacci numbers
        // in given range
        int result = 0;
     
        while (f1 <= high)
        {
            if (f1 >= low)
            result++;
            f1 = f2;
            f2 = f3;
            f3 = f1 + f2;
        }
     
        return result;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        int low = 10, high = 100;
        Console.WriteLine("Count of Fibonacci Numbers is "
                        + countFibs(low, high));
    }
}
     
// This code is contributed by Sam007.


PHP
= $low)
        $result++;
        $f1 = $f2;
        $f2 = $f3;
        $f3 = $f1 + $f2;
    }
 
    return $result;
}
 
// Driver Code
$low = 10; $high = 100;
echo "Count of Fibonacci Numbers is ",
               countFibs($low, $high);
 
// This code is contributed by nitin mittal.
?>


Javascript


输出 :

Count of Fibonacci Numbers is 5