Java program to find number of digits in Nth Fibonacci number
Hello programmers, in this tutorial you are going to learn Java program to find number of digits in Nth Fibonacci number by using Binet’s Formula. In this tutorial you will find how to get the nth element of the Fibonacci series and then most importantly you will learn how you can use the Binet’s formula to calculate the number of digits of the Nth number.
Binet’s Formula
Count of digits in Fib(n) = Log10Fib(n)
= Log10(Φn / √5)
= n*Log10(Φ) – Log10√5
= n*Log10(Φ) – (Log105)/2
Source code For Java program to find number of digits in Nth Fibonacci number
public class NumbOfDigitFib{ public static void main (String[] args) { int n = 12; int fibNumber = printFib(n); System.out.println(n + "th Fibonacci number from the series is " + fibNumber); System.out.println("No of Digits in the " + n +"th number is " +NumOfDigitsFib(n)); } public static int printFib(int n){ int x=0,y=0,z=1; for(int i = 1; i <= n-1; i++) { x = y; y = z; z = x + y; } return z; } static double NumOfDigitsFib(double n) { if (n == 1) return 1; // Formula used (n * log(phi) - (log 5) / 2) // using phi = 1.6180339887498948 double result = (n * Math.log10(1.6180339887498948)) - ((Math.log10(5)) / 2); return Math.ceil(result); } }
OUTPUT
12th Fibonacci number from the series is 144 No of Digitts in the 12th number is 3.0
How to code for finding the number of digits in the Nth Fibonacci number in JAVA?
Detailed Explanation:
- First, you need to create a class called NumbOfDigitFib in that you need to make three methods :
- Main Method: public static void main (String[] args){}
- printFab Method: public static int printFib(int n){}
- NumOfDigitsFab Method: static double NumOfDigitsFib(double n){}
The Main Method
- In the main Method first, you need to declare a variable named “n” and initialize it to 12.
- After that, you need to call printFib Method while passing the value of n as an argument and store the result of printFib Method in an integer ‘fibNumber’.
- Then you have to print the fibNumber variable with the initialized variable n in a print statement.
- Similarly, you need to write one more print statement wherein you have to call NumOfDigitsFib while passing integer n into it. And then directly print the result of the NumOfDigitFib method.
public static void main (String[] args) { int n = 12; int fibNumber = printFib(n); System.out.println(n + "th Fibonacci number from the series is " + fibNumber); System.out.println("No of Digits in the " + n +"th number is " +NumOfDigitsFib(n)); }
The printFib Method
- Here, in this method first, you need to write a parameter of int n with the method name.
public static int printFib(int n)
- After that, you need to create three variables of int type and initialize them.
int x=0,y=0,z=1;
- Then write a for loop from int i =0 to i <= n-1. with the value of i incrementing as i++.The For loop will calculate and generate the Fibonacci series for all the values i till n-1.
- The calculation is done by typing:
x = y; y = z; z = x + y;
- Then you need to return the value of z.
- In conclusion, The “nth” number result from the Fibonacci series is the n-1th number in for loop for the Fibonacci series.
public static int printFib(int n){ int x=0,y=0,z=1; for(int i = 1; i <= n-1; i++) { x = y; y = z; z = x + y; } return z; }
The NumOfDigitsFib Method
- Here, in this method first, you need to write a parameter of double n with the method name.
static double NumOfDigitsFib(double n)
- Now you have to write an if condition to check whether the value of n is 1 or not. If it’s 1, then you have to return 1.
if (n == 1)
return 1;
- Here in order to calculate the number of digits you have to use a formula that is: log(phi) – (log 5) / 2). Where the value of phi is 1.6180339887498948
- Now create a variable “result” of type double and store the calculated result of the formula that is:
double result = (n * Math.log10 (1.6180339887498948)) - ((Math.log10(5)) / 2);
- Now use the Math.ceil() method which is used to find the smallest integer value that is greater than or equal to the argument or mathematical integer. And pass result in it.
- Then simultaneously you need to return Mat.ceil(rseult).
static double NumOfDigitsFib(double n) { if (n == 1) return 1; // Formula used (n * log(phi) - (log 5) / 2) // using phi = 1.6180339887498948 double result = (n * Math.log10(1.6180339887498948)) - ((Math.log10(5)) / 2); return Math.ceil(result); }
Leave a Reply