075 Singular integer right triangles.jl 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/julia
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 18 August 2016
  4. # License: GPLv3
  5. # Website: https://github.com/trizen
  6. # https://projecteuler.net/problem=75
  7. # Runtime: 0.480s
  8. function count_singular_triples(limit::Int64)
  9. triangle = Dict{Int64, Int64}()
  10. upto = isqrt(limit)
  11. for n in 1:upto-1
  12. for m in n+1:2:upto
  13. x = (m^2 - n^2)
  14. y = (2 * m * n)
  15. z = (m^2 + n^2)
  16. x+y+z > limit && break
  17. if gcd(n, m) == 1
  18. k = 1
  19. while true
  20. x2 = k * x
  21. y2 = k * y
  22. z2 = k * z
  23. p = x2 + y2 + z2
  24. p > limit && break
  25. if !haskey(triangle, p)
  26. triangle[p] = 1
  27. else
  28. triangle[p] += 1
  29. end
  30. k += 1
  31. end
  32. end
  33. end
  34. end
  35. count = 0
  36. for v in values(triangle)
  37. v == 1 && (count += 1)
  38. end
  39. count
  40. end
  41. println(count_singular_triples(1_500_000))