![]() |
VOOZH | about |
Given a number ānā, write a function that prints the last two digits of n-th (ānā can also be a large number) Fibonacci number.
Examples:
Input : n = 65 Output : 65 Input : n = 365 Output : 65
Recommended: Please solve it on āā first, before moving on to the solution.
A simple solution is to find n-th Fibonacci number and print its last two digit. But N can be very large, so it wouldn't work.
A better solution is to use the fact that after 300-th Fibonacci number last two digits starts repeating.
1) Find m = n % 300.
2) Return m-th Fibonacci number.
Output:
1 61 13 53
Time Complexity: O(300), it will run in constant time.
Auxiliary Space: O(300), no extra space is required, so it is a constant.
Approach 2: Iterative Approach:
In this implementation, we simply iterate through the Fibonacci sequence up to the n-th number, keeping track of the last two digits of each number using the modulo operator. This allows us to compute the last two digits of even large Fibonacci numbers without needing to store all of the previous numbers in the sequence.
Here Are steps of this approach:
1 61 13 53
Time Complexity: O(N).
Auxiliary Space: O(1), no extra space is required, so it is a constant.