#!/usr/bin/env python3
"""Check, with plain integer arithmetic, that n^k + n + 1 is composite for every k > 1 when
n = 243779134570275685188 (and likewise for a second, larger example).

For each k the script exhibits an explicit proper divisor.  The divisibility pattern is periodic
in k (period 180 for the first n), so checking k up to a few periods covers every k.
"""
from math import lcm

def order(a, p):
    o, x = 1, a % p
    while x != 1: x = x * a % p; o += 1
    return o

def check(n, primes, k2_prime):
    assert n % 3 != 1
    Q = n * n + n + 1
    assert Q % k2_prime == 0 and 1 < k2_prime < Q
    period = lcm(3, *[order(n, p) for p in primes])
    for k in range(2, 3 * period + 3):
        if k == 2: continue                                   # divisible by k2_prime, checked above
        if k % 3 == 2: assert (pow(n, k, Q) + n + 1) % Q == 0; continue      # proper divisor n^2+n+1
        assert any((pow(n, k, p) + n + 1) % p == 0 for p in primes), k        # a small prime divides
    print(f"n = {n}: n^k + n + 1 has an explicit proper divisor for every k > 1 (period {period})")

check(243779134570275685188, [5, 7, 11, 13, 19, 31, 37, 331, 757, 3511, 9091], 43)
check(5010689049134674879809162, [7, 241, 11, 31, 13, 61, 421, 5, 17, 2161, 41, 331, 661, 73], 19)
