# Fibonacci Number modulo M and Pisano Period

Given two number **N** and **M**. The task is to find the **N-th** fibonacci number **mod** **M**.

In general let **F _{N}** be the

**N-th**fibonacci number then the output should be

**F**%

_{N}**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:

F_{0}= 0 F_{1}= 1 F_{n}= F_{n-1}+ F_{n-2}

These nos. are in the following sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

Here **N** can be large.

**Examples:**

Input:N = 438, M = 900

Output:44

Input:N = 1548276540, M = 235

Output:185

**Approach: **

However, for such values of *N*, a simple recursive approach to keep calculating *N* Fibonacci numbers with a time complexity of *O(2 ^{N})* 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 *F _{i} modulo M* (

`for i in range(N)`

) is periodic.**The Pisano Period is defined as the length of the period of this series.**

The period always starts with 01.

The period always starts with 01.

To understand it further, let’s see what happens when *M* is small:

i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
---|---|---|---|---|---|---|---|---|---|---|---|---|

F_{i} |
0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 |

F_{i} mod 2 |
0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |

F_{i} 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.

**Example: **

So to compute, say F_{2019} 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, F_{2019} mod 5 = F_{19} 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 *F _{N} modulo M* in Python:

`# Python 3 program to calculate ` `# Fibonacci no. modulo m using ` `# Pisano Period ` ` ` `# Calculate and return Pisano Period ` `# The length of a Pisano Period for ` `# a given m ranges from 3 to m * m ` `def` `pisanoPeriod(m): ` ` ` `previous, current ` `=` `0` `, ` `1` ` ` `for` `i ` `in` `range` `(` `0` `, m ` `*` `m): ` ` ` `previous, current \ ` ` ` `=` `current, (previous ` `+` `current) ` `%` `m ` ` ` ` ` `# A Pisano Period starts with 01 ` ` ` `if` `(previous ` `=` `=` `0` `and` `current ` `=` `=` `1` `): ` ` ` `return` `i ` `+` `1` ` ` `# Calculate Fn mod m ` `def` `fibonacciModulo(n, m): ` ` ` ` ` `# Getting the period ` ` ` `pisano_period ` `=` `pisanoPeriod(m) ` ` ` ` ` `# Taking mod of N with ` ` ` `# period length ` ` ` `n ` `=` `n ` `%` `pisano_period ` ` ` ` ` `previous, current ` `=` `0` `, ` `1` ` ` ` ` `for` `i ` `in` `range` `(n` `-` `1` `): ` ` ` `previous, current \ ` ` ` `=` `current, previous ` `+` `current ` ` ` ` ` `return` `(current ` `%` `m) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `1548276540` ` ` `m ` `=` `235` ` ` `print` `(fibonacciModulo(n, m)) ` |

*chevron_right*

*filter_none*

**Output:**

185

Pisano Period of 235 is 160. 1548276540 mod 160 is 60. F_{60} 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 calcuate F_{N} modulo *M*.

** Time Complexity:** O(M

^{2})

## Recommended Posts:

- Fibonacci modulo p
- Check if a M-th fibonacci number divides N-th fibonacci number
- Find sum of modulo K of first N natural number
- Primitive root of a prime number n modulo n
- Expressing a fraction as a natural number under modulo 'm'
- Find the number of primitive roots modulo prime
- Number of ways to represent a number as sum of k fibonacci numbers
- Finding number of digits in n'th Fibonacci number
- Nth XOR Fibonacci number
- Nth Even Fibonacci Number
- Find the next fibonacci number
- Fibonacci number in an array
- Count the nodes whose sum with X is a Fibonacci number
- Python Program for n-th Fibonacci number
- Program to find Nth odd Fibonacci Number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.