project-euler/127_abc-hits.py

47 lines
918 B
Python
Raw Normal View History

2023-03-26 17:21:44 +02:00
#!/usr/bin/env python3
from math import gcd
from collections import defaultdict
def getreps(n):
sieve = [1] * n
yield 1, 1
for p in range(2, n):
if sieve[p] == 1:
for i in range(p, n, p):
sieve[i] *= p
yield p, sieve[p]
N = 120000
repunits = list(getreps(N))
sortedreps = sorted(set(list(zip(*repunits))[1]))
repmap = defaultdict(list)
for n, rep in repunits:
repmap[rep].append(n)
count = 0
s = 0
for c, repC in repunits:
root = c // repC
if root <= 2:
continue
for repA in sortedreps:
if repA >= root:
break
if gcd(repA, repC) != 1:
continue
for a in repmap[repA]:
if 2*a >= c:
break
b = c - a
repB = repunits[b-1][1]
if repA*repB >= root:
continue
count += 1
s += c
print(s)