Read the checking program (131 lines of Python)
"""Prove primality of every term of an arithmetic progression and score its number of primes."""
import json
import sys
from math import gcd
MAX_BYTES = 32768
MAX_DIGITS = 155
MAX_TERM = 1 << 512
LENGTH = 27
UINT64 = 1 << 64
MAX_WITNESS_CHECKS = 256
def decimal(token, digits=MAX_DIGITS):
if len(token) > digits or not token.isdigit():
raise ValueError("Use unsigned decimal integers within the digit limit.")
return int(token)
def is_prime64(n):
"""These seven Miller–Rabin bases are exhaustive below 2**64."""
if n < 2 or n >= UINT64:
return False
if n % 2 == 0:
return n == 2
odd = n - 1
twos = (odd & -odd).bit_length() - 1
odd >>= twos
for base in (2, 325, 9375, 28178, 450775, 9780504, 1795265022):
base %= n
if base == 0:
continue
residue = pow(base, odd, n)
if residue in (1, n - 1):
continue
for _ in range(twos - 1):
residue = residue * residue % n
if residue == n - 1:
break
else:
return False
return True
def certified_primes(lines, largest):
"""Check Pocklington proofs in increasing order of prime candidate."""
proven = set()
previous = UINT64 - 1
checks = 0
for line in lines:
tokens = line.split()
if len(tokens) < 3:
raise ValueError("A prime proof needs a candidate, a witness and prime powers.")
checks += len(tokens) - 1
if checks > MAX_WITNESS_CHECKS:
raise ValueError("The submission exceeds the prime proof work limit.")
n, witness = map(decimal, tokens[:2])
if not previous < n <= largest or not 1 < witness < n or n % 2 == 0:
raise ValueError("A prime proof candidate or witness is out of range.")
previous = n
# Peel off complete prime powers of n−1. The remaining cofactor may
# be unknown. Every prime divisor p of n must have F dividing p−1.
remaining = n - 1
distinct = set()
for token in tokens[2:]:
parts = token.split(b"^", 1)
if len(parts) != 2:
raise ValueError("Write each prime power as q^e.")
q, e = decimal(parts[0]), decimal(parts[1], 4)
if q in distinct or not (q in proven or is_prime64(q)):
raise ValueError("Each prime power needs a distinct, already proven prime.")
if e < 1 or (q.bit_length() - 1) * e > n.bit_length():
raise ValueError("A prime power exponent is out of range.")
power = q ** e
remaining, rest = divmod(remaining, power)
if rest or remaining % q == 0:
raise ValueError("Each prime power must divide the candidate minus one exactly.")
distinct.add(q)
factored_part = (n - 1) // remaining
if factored_part * factored_part <= n:
raise ValueError("The proven prime powers must multiply to more than the square root.")
if pow(witness, n - 1, n) != 1 or any(
gcd(pow(witness, (n - 1) // q, n) - 1, n) != 1 for q in distinct
):
raise ValueError("The prime proof witness fails Pocklington's congruences.")
proven.add(n)
return proven
def verify(data, length=LENGTH):
lines = data.strip().splitlines()
terms = lines[0].split() if lines else []
if len(terms) != 3:
raise ValueError("Give start, step and length on the first line.")
start, step = map(decimal, terms[:2])
count = decimal(terms[2], 3)
if start < 2 or step < 1:
raise ValueError("The start must be at least two and the step must be positive.")
if not 3 <= count <= 512:
raise ValueError("The progression must have between three and 512 terms.")
end = start + (count - 1) * step
if end >= MAX_TERM:
raise ValueError("All progression terms must fit in 512 bits.")
proven = certified_primes(lines[1:], end)
for n in range(start, end + 1, step):
if n not in proven and not is_prime64(n):
raise ValueError("A progression term is composite or missing a prime proof.")
return count
def main():
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("The submission exceeds the byte limit.")
if not data.isascii() or any(c < 32 and c not in (9, 10, 13) for c in data):
raise ValueError("Use plain ASCII text.")
score = verify(data)
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
else:
result = {"valid": True, "score": score, "message": "All terms in the progression are proven prime."}
print(json.dumps(result))
if __name__ == "__main__":
main()