20
1
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!
3Is the cycle always formed by reaching the initial values again? Or is it possible that there's a cycle that does not include the initial values? If yes, it would be good to have a test example for this case. I haven't found one with a few random attempts, but I don't have the mathematical skills to prove that the values will always go back to the initial values. – Reto Koradi – 2015-07-18T14:30:53.393
3@RetoKoradi Cycles must always include initial values, because there can't be any branching. Suppose that the first number in the cycle is A_i, and let the number behind A_i in the cycle be X. A_i+1 - A_i = A_i-1, but it also equals X, so A_i-1 equals X, a contradiction. – lirtosiast – 2015-07-18T14:52:51.600
Possibly of interest: http://math.stackexchange.com/questions/1358310/how-to-prove-that-the-fibonacci-sequence-is-periodic-mod-5-without-using-inducti
– mathmandan – 2015-07-18T15:52:17.153