748 Upside Down Diophantine Equation -- v2.py 954 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/python
  2. # Upside down Diophantine equation
  3. # https://projecteuler.net/problem=748
  4. from math import isqrt
  5. def generate_primitive_solutions(N):
  6. solutions = set()
  7. for v in range(1, isqrt(N // 13) + 1):
  8. v_squared_13 = v**4 * 13
  9. for x in range(1, isqrt(v_squared_13) + 1):
  10. if v_squared_13 % x == 0:
  11. y_squared = v_squared_13 // x
  12. x_squared_plus_y_squared = x**2 + y_squared
  13. if x_squared_plus_y_squared <= N:
  14. z_squared = x**2 * y_squared // x_squared_plus_y_squared
  15. z = isqrt(z_squared)
  16. solutions.add((x, isqrt(y_squared), z))
  17. return solutions
  18. def S(N):
  19. primitive_solutions = generate_primitive_solutions(N)
  20. return sum(sum(solution) for solution in primitive_solutions)
  21. # Calculate S(10^16) mod 10^9
  22. #result = S(10**16) % 10**9
  23. result = S(10**5) % 10**9 #=> 79635 (wrong)
  24. print(result)