📌  相关文章
📜  在第n个斐波那契数中查找位数

📅  最后修改于: 2021-05-07 07:56:24             🧑  作者: Mango

给定数字n,请在第n个斐波那契数字中找到数字位数。前几个斐波那契数是0、1、1、2、3、5、8、13、21、34、55、89、144,…。
例子:

Input : n = 6
Output : 1
6'th Fibonacci number is 8 and it has 
1 digit.

Input : n = 12
Output : 3
12'th Fibonacci number is 144 and it has 
3 digits.

一个简单的解决方案是找到第n个斐波那契数,然后计算其中的位数。对于大的n值,此解决方案可能导致溢出问题。
一种直接的方法是使用下面的Binet公式对第n个斐波那契数中的位数进行计数。

fib(n) = (Φn - Ψ-n) / √5
where
Φ = (1 + √5) / 2
Ψ = (1 - √5) / 2

The above formula can be simplified, 
fib(n) = round(Φn / √5) 
Here round function indicates nearest integer.

Count of digits in Fib(n) = Log10Fib(n)
                          = Log10(Φn / √5)
                          = n*Log10(Φ) - Log10√5
                          = n*Log10(Φ) - (Log105)/2

正如《 G事实》所述,由于浮点运算的限制,该公式似乎无法正常工作,无法产生正确的斐波那契数。但是,使用此公式查找第n个斐波纳契数的位数似乎是可行的。
下面是上述想法的实现:

C++
/* C++ program to find number of digits in nth
   Fibonacci number */
#include
using namespace std;
 
// This function returns the number of digits
// in nth Fibonacci number after ceiling it
// Formula used (n * log(phi) - (log 5) / 2)
long long numberOfDigits(long long n)
{
    if (n == 1)
        return 1;
 
    // using phi = 1.6180339887498948
    long double d = (n * log10(1.6180339887498948)) -
                    ((log10(5)) / 2);
 
    return ceil(d);
}
 
// Driver program to test the above function
int main()
{
    long long i;
    for (i = 1; i <= 10; i++)
    cout << "Number of Digits in F("
         << i <<") - "
         << numberOfDigits(i) << "\n";
 
    return 0;
}


Java
// Java program  to find number of digits in nth
// Fibonacci number
 
class GFG
{
    // This function returns the number of digits
    // in nth Fibonacci number after ceiling it
    // Formula used (n * log(phi) - (log 5) / 2)
    static double numberOfDigits(double n)
    {
        if (n == 1)
            return 1;
     
        // using phi = 1.6180339887498948
        double d = (n * Math.log10(1.6180339887498948)) -
                   ((Math.log10(5)) / 2);
     
        return Math.ceil(d);
    }
 
    // Driver code
    public static void main (String[] args)
    {
        double i;
        for (i = 1; i <= 10; i++)
        System.out.println("Number of Digits in F("+i+") - "
                           +numberOfDigits(i));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# number of digits in nth
# Fibonacci number
import math
 
# storing value of
# golden ratio aka phi
phi = (1 + 5**.5) / 2
 
# function to find number
# of digits in F(n) This
# function returns the number
# of digitsin nth Fibonacci
# number after ceiling it
# Formula used (n * log(phi) -
# (log 5) / 2)
def numberOfDig (n) :
    if n == 1 :
        return 1
    return math.ceil((n * math.log10(phi) -
                      .5 * math.log10(5)))
 
// Driver Code
for i in range(1, 11) :
    print("Number of Digits in F(" +
                   str(i) + ") - " +
                str(numberOfDig(i)))
 
# This code is contributed by SujanDutta


C#
// C# program to find number of
// digits in nth Fibonacci number
using System;
 
class GFG {
     
    // This function returns the number of digits
    // in nth Fibonacci number after ceiling it
    // Formula used (n * log(phi) - (log 5) / 2)
    static double numberOfDigits(double n)
    {
        if (n == 1)
            return 1;
     
        // using phi = 1.6180339887498948
        double d = (n * Math.Log10(1.6180339887498948)) -
                   ((Math.Log10(5)) / 2);
     
        return Math.Ceiling(d);
    }
 
    // Driver code
    public static void Main ()
    {
        double i;
        for (i = 1; i <= 10; i++)
        Console.WriteLine("Number of Digits in F("+ i +") - "
                           + numberOfDigits(i));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


输出:

Number of Digits in F(1) - 1
Number of Digits in F(2) - 1
Number of Digits in F(3) - 1
Number of Digits in F(4) - 1
Number of Digits in F(5) - 1
Number of Digits in F(6) - 1
Number of Digits in F(7) - 2
Number of Digits in F(8) - 2
Number of Digits in F(9) - 2
Number of Digits in F(10) - 2