Given a number n, find n-th Fibonacci Number. Note that n may be large.
Input : 100 Output : 218922995834555169026 Input : 500 Output : 8616829160023845073278831216566478809594 1068326060883324529903470149056115823592 713458328176574447204501
Fibonacci of large number may contain more than 100 digits, it can be easily handled by BigInteger in Java. BigInteger class is used for mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types.
268638100244853593861467272021429239676 166093189869523401231759976179817002478 816893383696544833565641918278561614433 563129766736422103503246348504103776803 673341511728991697231970827639856157644 50078474174626
Note that the above solution takes O(n) time, we can find n-th Fibonacci number in O(Log n) time. As an exercise, find n-th Fibonacci number for large n in O(Log n) time.
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
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.
- Generating large Fibonacci numbers using boost library
- GCD of two numbers when one of them can be very large
- Sum of two large numbers
- LCM of two large numbers
- Remainder with 7 for large numbers
- Difference of two large numbers
- Divisible by 37 for large numbers
- Sum of all natural numbers from L to R ( for large values of L and R )
- Find Last Digit of a^b for Large Numbers
- Sum of first N natural numbers when N is extremely large
- Sum of two large Floating-point numbers
- Sort an array of large numbers
- Fermat's Factorization method for large numbers
- Last digit of Product of two Large or Small numbers (a * b)
- Multiply Large Numbers using Grid Method
- How to generate Large Prime numbers for RSA Algorithm
- Writing power function for large numbers
- Factorial of Large numbers using Logarithmic identity
- Multiply Large Numbers represented as Strings
- Sum of Fibonacci Numbers