euler/python/e127.py

68 lines
1.5 KiB
Python
Raw Normal View History

2024-03-06 02:00:31 +01:00
from functools import lru_cache
from lib_prime import prime_factors
@lru_cache(maxsize=200000)
def unique_factors(n: int) -> set[int]:
return set(prime_factors(n))
def euler_127():
c_max = 120000
r = 0
# for a in [5]:
for a in range(1, c_max):
fsa = unique_factors(a)
# for b in [27]:
for b in range(a + 1, c_max):
if a + b > c_max:
break
c = a + b
rad = 1
do_continue = False
for fa in fsa:
rad *= fa
if b % fa == 0:
do_continue = True
break
if c % fa == 0:
do_continue = True
break
if do_continue:
continue
if rad > c:
continue
do_continue = False
fsb = unique_factors(b)
for fb in fsb:
rad *= fb
if c % fb == 0:
do_continue = True
break
if do_continue:
continue
if rad >= c:
continue
fsc = unique_factors(c)
for fc in fsc:
rad *= fc
if rad >= c:
continue
# print(fsa, fsb, fsc)
# print(a, b, c)
r += c
return r
if __name__ == "__main__":
solution = euler_127()
print("e127.py: " + str(solution))
assert(solution == 18407904)