The Fibonacci sequence is a well known and famous sequence, where each successive element is the sum of the previous two. A modular Fibonacci sequence is the same, except that the addition is performed over a finite field, or to put it another way, a modulo operation is performed after each addition.
For instance, the modular Fibonacci sequence with starting values 1, 1
and modulus 5
is:
1, 1, 2, 3, 0, 3, 3, 1, 4, 0, 4, 4, 3, 2, 0, 2, 2, 4, 1, 0, 1, 1, ...
At this point, the sequence is once again 1, 1
, and so the sequence will repeat, cyclically, with a cycle length of 20.
Your challenge is to find the (minimal) cycle length for a specific starting pair and modulus, in the fewest bytes.
Details: The initial values will both be in the range 0 <= i,j < m
, where m
is the modulus. The initial values will not both be zero, and the modulus will be at least 2. You may take the input in any standard means or format, in any order, function, program or similar, and output via return or print. Standard loopholes apply.
Examples:
First element, second element, modulus:
1, 1, 5 => 20
1, 1, 4 => 6
2, 4, 6 => 8
3, 3, 6 => 3
This is code golf - shortest code in bytes wins. Good luck!