aboutsummaryrefslogtreecommitdiff
path: root/challenge-155/paulo-custodio/python/ch-2.py
blob: f5408b05dbc19a198e9d4e8f18897344eee0a45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3

# Challenge 155
#
# TASK #2 > Pisano Period
# Submitted by: Mohammad S Anwar
# Write a script to find the period of the 3rd Pisano Period.
#
# In number theory, the nth Pisano period, written as p(n), is the period with
# which the sequence of Fibonacci numbers taken modulo n repeats.
#
# The Fibonacci numbers are the numbers in the integer sequence:
#
# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, ...
# For any integer n, the sequence of Fibonacci numbers F(i) taken modulo n is
# periodic. The Pisano period, denoted p(n), is the value of the period of this
# sequence. For example, the sequence of Fibonacci numbers modulo 3 begins:
#
# 0, 1, 1, 2, 0, 2, 2, 1,
# 0, 1, 1, 2, 0, 2, 2, 1,
# 0, 1, 1, 2, 0, 2, 2, 1, ...
# This sequence has period 8, so p(3) = 8.

from math import isqrt

order = 3

def fibonacci_series(n):
    fibs = [0, 1]
    for i in range(2, n):
        fibs.append(fibs[-1] + fibs[-2])
    return fibs

def is_period(period, n):
    base = n[0:period]
    ord1 = n[period:2*period]
    ord2 = n[2*period:3*period]

    if base != ord1:
        return 0
    if base != ord2:
        return 0
    return 1

def find_period(n):
    period = 1
    while True:
        if 3 * period > len(n):
            return 0
        if is_period(period, n):
            return period
        period += 1
    return 0

fibs = fibonacci_series(100)
fibs_mod = [fib % order for fib in fibs]
print(find_period(fibs_mod))