project-euler/141_progressive_numbers.py

30 lines
501 B
Python

#!/usr/bin/env python3
from math import gcd
def is_square(n):
return int(n**0.5)**2 == n
def sol(p, q, n):
return n*q * (p**3 * n + q)
N = 10**12
a = 1
r = 0
for p in range(2, int(N**(1.0/3))):
for q in range(1, p):
if p**3 * q + q**2 >= N:
break
if gcd(p, q) > 1:
continue
n = 1
s = sol(p, q, n)
while s < N:
if is_square(s):
r += s
n += 1
s = sol(p, q, n)
print(r)