Given a number n, find number of digits in n’th Fibonacci Numbers. First few Fibinacci numbers are 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.
A simple solution is to find n’th Fibonacci Number and then count number of digits in it. This solution may lead to overflow problems for large values of n.
A direct way is to count number of digits in the nth Fibonacci number using below Binet’s Formula.
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
As mentioned in this G-Fact, this formula doesn’t seem to work and produce correct Fibonacci numbers due to limitations of floating point arithmetic. However, it looks viable to use this formula to find count of digits in n’th Fibonacci number.
Below is the implementation of above idea :
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
This article is contributed by Ayush Khanduri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- G-Fact 18 | Finding nth Fibonacci Number using Golden Ratio
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Finding sum of digits of a number until sum becomes single digit
- Program to find last two digits of Nth Fibonacci number
- Check if a M-th fibonacci number divides N-th fibonacci number
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Number of ways to represent a number as sum of k fibonacci numbers
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- How to check if a given number is Fibonacci number?
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find smallest possible Number from a given large Number with same count of digits
- Count number of digits after decimal on dividing a number
- Number of digits to be removed to make a number divisible by 3
- Print a number strictly less than a given number such that all its digits are distinct.
- Count of prime digits of a Number which divides the number
- Find maximum number that can be formed using digits of a given number