Given two number N and M. The task is to find the N-th fibonacci number mod M.
In general let FN be the N-th fibonacci number then the output should be FN % M.
The Fibonacci sequence is a series of numbers in which each no. is the sum of two preceding nos. It is defined by the recurrence relation:
F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2
These nos. are in the following sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
Here N can be large.
Input: N = 438, M = 900
Input: N = 1548276540, M = 235
However, for such values of N, a simple recursive approach to keep calculating N Fibonacci numbers with a time complexity of O(2N) should be avoided. Even an iterative or a Dynamic Programming approach with an algorithm looping for N iterations will not be time-efficient.
This problem can be solved using the properties of Pisano Period.
For a given value of N and M >= 2, the series generated with Fi modulo M (for i in range(N)) is periodic.
The period always starts with 01. The Pisano Period is defined as the length of the period of this series.
To understand it further, let’s see what happens when M is small:
|Fi mod 2||0||1||1||0||1||1||0||1||1||0||1||1|
|Fi mod 3||0||1||1||2||0||2||2||1||0||1||1||2|
For M = 2, the period is 011 and has length 3 while for M = 3 the sequence repeats after 8 nos.
So to compute, say F2019 mod 5, we’ll find the remainder of 2019 when divided by 20 (Pisano Period of 5 is 20). 2019 mod 20 is 19. Therefore, F2019 mod 5 = F19 mod 5 = 1. This property is true in general.
We need to find the remainder when N is divided by the Pisano Period of M. Then calculate F(N)remainder mod M for the newly calculated N.
Below is the implementation of FN modulo M in Python:
Pisano Period of 235 is 160. 1548276540 mod 160 is 60. F60 mod 235 = 185. Using Pisano Period, we now need to calculate Fibonacci nos. iteratively for a relatively lower N than specified in the original problem and then calculate FN modulo M.
Time Complexity: O(M2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Fibonacci modulo p
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Find length of period in decimal value of 1/n
- Number of factors of very large number N modulo M where M is any prime number
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Find sum of modulo K of first N natural number
- Primitive root of a prime number n modulo n
- Find the number of primitive roots modulo prime
- Expressing a fraction as a natural number under modulo 'm'
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Find the value of P and modular inverse of Q modulo 998244353
- Count possible values of K such that X - 1 and Y - 1 modulo K is same
- Compute n! under modulo p
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Maximum subarray sum modulo m
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.