Given a number n, find number of digits in n’th Fibonacci Numbers. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ….
Examples:
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 :
C++
#include<bits/stdc++.h>
using namespace std;
long long numberOfDigits( long long n)
{
if (n == 1)
return 1;
long double d = (n * log10 (1.6180339887498948)) -
(( log10 (5)) / 2);
return ceil (d);
}
int main()
{
long long i;
for (i = 1; i <= 10; i++)
cout << "Number of Digits in F("
<< i << ") - "
<< numberOfDigits(i) << "\n" ;
return 0;
}
|
Java
class GFG
{
static double numberOfDigits( double n)
{
if (n == 1 )
return 1 ;
double d = (n * Math.log10( 1.6180339887498948 )) -
((Math.log10( 5 )) / 2 );
return Math.ceil(d);
}
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));
}
}
|
Python3
import math
phi = ( 1 + 5 * * . 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)))
|
C#
using System;
class GFG {
static double numberOfDigits( double n)
{
if (n == 1)
return 1;
double d = (n * Math.Log10(1.6180339887498948)) -
((Math.Log10(5)) / 2);
return Math.Ceiling(d);
}
public static void Main ()
{
double i;
for (i = 1; i <= 10; i++)
Console.WriteLine( "Number of Digits in F(" + i + ") - "
+ numberOfDigits(i));
}
}
|
Javascript
<script>
function numberOfDigits(n)
{
if (n == 1)
return 1;
let d = (n * Math.log10(1.6180339887498948)) -
((Math.log10(5)) / 2);
return Math.ceil(d);
}
let i;
for (let i = 1; i <= 10; i++)
document.write(`Number of Digits in F(${i}) - ${numberOfDigits(i)} <br>`);
</script>
|
PHP
<?php
function numberOfDigits( $n )
{
if ( $n == 1)
return 1;
$d = ( $n * log10(1.6180339887498948)) -
((log10(5)) / 2);
return ceil ( $d );
}
$i ;
for ( $i = 1; $i <= 10; $i ++)
echo "Number of Digits in F($i) - "
, numberOfDigits( $i ), "\n" ;
?>
|
OutputNumber 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
Time Complexity: O(1)
Auxiliary Space: O(1)
Another Approach(Using fact that Fibonacci numbers are periodic):
Fibonacci sequence is periodic modulo any integer, with period equal to 60 (known as Pisano’s period). This means that we can calculate the nth Fibonacci number modulo 10^k for some large k, and then use the periodicity to compute the number of digits. For example, we can compute F_n modulo 10^10 and count the number of digits:
F_n_mod = F_n % 10**10
digits = floor(log10(F_n_mod)) + 1
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
long long numberOfDigits( long long n){
int k = 10;
int phi = (1 + sqrt (5)) / 2;
int a = 0, b = 1;
for ( int i = 2; i <= n; i++) {
int c = (a + b) % int ( pow (10, k));
a = b;
b = c;
}
int F_n_mod = b;
int digits = 1;
while (F_n_mod >= 10) {
F_n_mod /= 10;
digits++;
}
return digits;
}
int main(){
long long i;
for (i = 1; i <= 10; i++)
cout << "Number of Digits in F("
<< i << ") - "
<< numberOfDigits(i) << "\n" ;
return 0;
}
|
Python3
import math
def numberOfDigits(n):
k = 10
phi = ( 1 + math.sqrt( 5 )) / 2
a, b = 0 , 1
for i in range ( 2 , n + 1 ):
c = (a + b) % pow ( 10 , k)
a = b
b = c
F_n_mod = b
digits = 1
while F_n_mod > = 10 :
F_n_mod = F_n_mod / / 10
digits + = 1
return digits
for i in range ( 1 , 11 ):
print ( "Number of Digits in F(" + str (i) + ") - " + str (numberOfDigits(i)))
|
Javascript
function numberOfDigits(n) {
let k = 10;
let phi = (1 + Math.sqrt(5)) / 2;
let a = 0,
b = 1;
for (let i = 2; i <= n; i++) {
let c = (a + b) % Math.pow(10, k);
a = b;
b = c;
}
let F_n_mod = b;
let digits = 1;
while (F_n_mod >= 10) {
F_n_mod = Math.floor(F_n_mod / 10);
digits++;
}
return digits;
}
let i;
for (i = 1; i <= 10; i++)
console.log( "Number of Digits in F(" + i + ") - " + numberOfDigits(i));
|
OutputNumber 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
Time Complexity: O(nk)
Auxiliary Space: O(1)
References:
http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html#section2
https://en.wikipedia.org/wiki/Fibonacci_number
This article is contributed by Ayush Khanduri. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.