"""Reproduce the four Fibonacci-sum checks in MathOverflow question 268901."""
from collections import Counter
from fractions import Fraction
from math import lcm

PAIRS = [(79, 859), (139, 461), (229, 95419), (263, 967)]


def period(modulus):
    left, right = 0, 1
    values = []
    while True:
        values.append(left)
        left, right = right, (left + right) % modulus
        if (left, right) == (0, 1):
            return values


def sums_below(bound):
    values = []
    left, right = 1, 2
    while left < bound:
        values.append(left)
        left, right = right, left + right
    return {x + y for i, x in enumerate(values) for y in values[i:] if x + y < bound}


def tally_by_period(a, b):
    residues_a, residues_b = period(a), period(b)
    length = lcm(len(residues_a), len(residues_b))
    product_values = period(a*b)
    assert len(product_values) == length
    counts = Counter()
    converse_failures = 0
    for i in range(length):
        for j in range(length):
            s = product_values[i] + product_values[j]
            if s % a == 0:
                counts[s % b] += 1
            if s % b == 0 and s % a:
                converse_failures += 1
    return len(residues_a), len(residues_b), counts, converse_failures


if __name__ == '__main__':
    bounded = sums_below(10**100)
    for a, b in PAIRS:
        per_a, per_b, counts, failures = tally_by_period(a, b)
        histogram = Counter(s % b for s in bounded if s % a == 0)
        print((a, b), 'periods', (per_a, per_b), 'exceptions to b implies a', failures)
        print('ordered index pairs', sorted(counts.items()))
        print('distinct sums below 10^100', sorted(histogram.items()))
        print('limiting fraction at zero among a-divisible sums', Fraction(counts[0], sum(counts.values())))
