764 Asymmetric Diophantine Equation.py 739 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/python
  2. # Asymmetric Diophantine Equation
  3. # https://projecteuler.net/problem=764
  4. from math import isqrt
  5. from sympy import gcd
  6. def generate_solutions(N):
  7. solutions = set()
  8. for y in range(1, isqrt(N) + 1):
  9. y_squared = y**2
  10. for x in range(1, isqrt(N // 4) + 1):
  11. x_squared = 16 * x**2
  12. z_squared = x_squared + y_squared**2
  13. z = isqrt(z_squared)
  14. if z**2 == z_squared and z <= N and gcd([x, y, z]) == 1:
  15. solutions.add((x, y, z))
  16. return solutions
  17. def S(N):
  18. solutions = generate_solutions(N)
  19. return sum(sum(solution) for solution in solutions) % 10**9
  20. # Calculate S(10^16) mod 10^9
  21. result = S(10**4) #=> 702 (wrong)
  22. print(result)