675 2 to omega of n.jl 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/julia
  2. # Daniel "Trizen" Șuteu
  3. # Date: 27 June 2019
  4. # https://github.com/trizen
  5. # Based on the identity:
  6. # Sum_{d|n} 2^omega(d) = sigma_0(n^2)
  7. # The algorithm iteraters over each number in k = 1..N,
  8. # and computes sigma_0(k^2) = Prod_{p^e | k} (2*e + 1).
  9. # By keeping track of the partial products, we find sigma_0(k!^2).
  10. # Runtime: 17.373s
  11. # https://projecteuler.net/problem=675
  12. using Primes
  13. using Printf
  14. function F(N::Int64, MOD::Int64)
  15. table = Dict{Int64,Int64}()
  16. total = 0
  17. partial = 1
  18. function mulmod(a,b)
  19. (a * b) % MOD
  20. end
  21. for k in 2:N
  22. old = 1 # old product
  23. for (p,e) in factor(k)
  24. if haskey(table, p)
  25. old = mulmod(old, 2*table[p] + 1)
  26. else
  27. table[p] = 0
  28. end
  29. table[p] += e
  30. partial = mulmod(partial, 2*table[p] + 1)
  31. end
  32. partial = mulmod(partial, invmod(old, MOD)) # remove the old product
  33. total += partial
  34. total %= MOD
  35. end
  36. return total
  37. end
  38. const MOD = 1000000087
  39. for n in 1:7
  40. @printf("F(10^%d) = %10d (mod %d)\n", n, F(10^n, MOD), MOD)
  41. end