516 5-smooth totients.jl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/julia
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 04 May 2017
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=516
  7. # Runtime: 1 min, 12 sec.
  8. using Primes
  9. function Φ(n::Int64)
  10. for p in keys(factor(n))
  11. n -= div(n, p)
  12. end
  13. n
  14. end
  15. function is_5_smooth(n::Int64)
  16. for p in [2,3,5]
  17. while (rem(n, p) == 0)
  18. n = div(n, p)
  19. end
  20. end
  21. n == 1
  22. end
  23. function p_516(limit::Int64 = 10^12)
  24. smooth = Int64[1]
  25. for p in [2,3,5]
  26. for n in smooth
  27. if (n*p <= limit)
  28. append!(smooth, n*p)
  29. end
  30. end
  31. end
  32. smooth = reverse(
  33. sort(
  34. filter((n)->isprime(n),
  35. map((n)->n+1, smooth))
  36. )
  37. )
  38. h = Int64[1]
  39. mod = 1 << 32
  40. sum = 0
  41. for p in smooth
  42. for n in h
  43. if (p*n <= limit && is_5_smooth(Φ(n*p)))
  44. if (p == 2)
  45. for n in h
  46. while (n*p <= limit)
  47. sum += (n*p) % mod
  48. sum %= mod
  49. n *= p
  50. end
  51. end
  52. break
  53. else
  54. sum += (n*p) % mod
  55. sum %= mod
  56. append!(h, n*p)
  57. end
  58. end
  59. end
  60. println("$p -> $sum")
  61. end
  62. return sum+1
  63. end
  64. println(p_516(10^12))