348 Sum of a square and a cube.jl 763 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/julia
  2. # Daniel "Trizen" Șuteu
  3. # Date: 20 February 2017
  4. # License: GPLv3
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=348
  7. # Runtime: 15.005s
  8. function p348(n)
  9. count = Dict{Int64, Int64}()
  10. total = 0
  11. for i in 1:2^32
  12. for j in 1:(i>>2)
  13. s = i^2
  14. c = j^3
  15. p = s+c
  16. if (string(p) == reverse(string(p)))
  17. if haskey(count, p)
  18. count[p] += 1
  19. else
  20. count[p] = 1
  21. end
  22. if (count[p] == 4)
  23. total += p
  24. n -= 1
  25. end
  26. end
  27. end
  28. n == 0 && break
  29. end
  30. return total
  31. end
  32. println("Sum: ", p348(5))