565 Divisibility of sum of divisors.jl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/julia
  2. # Daniel "Trizen" Șuteu
  3. # Date: 04 July 2018
  4. # https://github.com/trizen
  5. # https://projecteuler.net/problem=565
  6. # Runtime: ~18 minutes
  7. using Primes
  8. function divisor_sum(n)
  9. sigma = 1
  10. for (p,e) in factor(n)
  11. s = 1
  12. for j in 1:e
  13. s += p^j
  14. end
  15. sigma *= s
  16. end
  17. sigma
  18. end
  19. function S(n::Int64, k::Int64)
  20. total = 0
  21. seen = Dict{Int64,Bool}()
  22. function process_term(t::Int64)
  23. for s in t:t:n
  24. if (s % (t*t) == 0)
  25. continue
  26. end
  27. if (divisor_sum(div(s,t)) % k == 0)
  28. if (haskey(seen, s))
  29. continue
  30. else
  31. seen[s] = true
  32. end
  33. end
  34. if (divisor_sum(s) % k == 0)
  35. total += s
  36. end
  37. end
  38. end
  39. p = 2
  40. isqrtn = isqrt(n)
  41. while (p <= isqrtn)
  42. for e in 3 : 1+Int64(floor(log(p, n)))
  43. if ((p^e - 1) % k == 0)
  44. process_term(p^(e-1))
  45. end
  46. end
  47. p = nextprime(p+1)
  48. end
  49. for t in k:k:n
  50. if (isprime(t - 1))
  51. process_term(t - 1)
  52. end
  53. end
  54. total
  55. end
  56. println(S(10^6, 2017))
  57. println(S(10^9, 2017))
  58. println(S(10^11, 2017))