This is a Java Program to Implement Efficient O(log n) Fibonacci generator . This is a program to generate nth fibonacci number with O(log n) complexity.
Here is the source code of the Java Program to Implement Efficient O(log n) Fibonacci generator . The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/** ** Java Program to Implement Efficient O(log n) Fibonacci generator **/ import java.util.Scanner; import java.math.BigInteger; /** Class FibonacciGenerator **/ public class FibonacciGenerator { /** function to generate nth fibonacci number **/ public void genFib(long n) { BigInteger arr1[][] = {{BigInteger.ONE, BigInteger.ONE}, {BigInteger.ONE, BigInteger.ZERO}}; if (n == 0) System.out.println("\nFirst Fibonacci number = 0"); else { power(arr1, n - 1); System.out.println("\n"+ n +" th Fibonacci number = "+ arr1[0][0]); } } /** function raise matrix to power n recursively **/ private void power(BigInteger arr1[][], long n) { if (n == 0 || n == 1) return; BigInteger arr2[][] = {{BigInteger.ONE, BigInteger.ONE}, {BigInteger.ONE, BigInteger.ZERO}}; power(arr1, n / 2); multiply(arr1, arr1); if (n % 2 != 0) multiply(arr1, arr2); } /** function to multiply two 2 d matrices **/ private void multiply(BigInteger arr1[][], BigInteger arr2[][]) { BigInteger x = (arr1[0][0].multiply(arr2[0][0])).add(arr1[0][1].multiply(arr2[1][0])); BigInteger y = (arr1[0][0].multiply(arr2[0][1])).add(arr1[0][1].multiply(arr2[1][1])); BigInteger z = (arr1[1][0].multiply(arr2[0][0])).add(arr1[1][1].multiply(arr2[1][0])); BigInteger w = (arr1[1][0].multiply(arr2[0][1])).add(arr1[1][1].multiply(arr2[1][1])); arr1[0][0] = x; arr1[0][1] = y; arr1[1][0] = z; arr1[1][1] = w; } /** Main function **/ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Efficient Fibonacci Generator\n"); System.out.println("Enter number n to find nth fibonacci number\n"); long n = scan.nextLong(); FibonacciGenerator fg = new FibonacciGenerator(); fg.genFib(n); } }
Output:
Efficient Fibonacci Generator Enter number n to find nth fibonacci number 1000 1000 th Fibonacci number = 43466557686937456435688527675040625802564660517371780 40248172908953655541794905189040387984007925516929592259308032263477520968962323 9873322471161642996440906533187938298969649928516003704476137795166849228875
Related posts:
Java Program to Implement Rolling Hash
Guava CharMatcher
REST Pagination in Spring
Custom Cascading in Spring Data MongoDB
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java program to Implement Tree Set
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
How to Get All Dates Between Two Dates?
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Entity To DTO Conversion for a Spring REST API
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Guava – Join and Split Collections
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Cài đặt và sử dụng Swagger UI
Java Program to Emulate N Dice Roller
Prevent Brute Force Authentication Attempts with Spring Security
Guide to Character Encoding
StringBuilder vs StringBuffer in Java
Java Program to Implement AVL Tree
Java Program to Implement SynchronosQueue API
Marker Interface trong Java
Java Program to Implement WeakHashMap API
Reading an HTTP Response Body as a String in Java
ExecutorService – Waiting for Threads to Finish
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Primitive Type Streams in Java 8
Concurrent Test Execution in Spring 5
Inheritance and Composition (Is-a vs Has-a relationship) in Java
A Guide to HashSet in Java
Guide to Java OutputStream
Working with Tree Model Nodes in Jackson