581 47-smooth triangular numbers.jl 1000 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/julia
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 02 May 2017
  5. # Edit: 28 June 2019
  6. # https://github.com/trizen
  7. # https://projecteuler.net/problem=581
  8. # Runtime: 3.492s.
  9. using Primes
  10. function p_581()
  11. smooth = 47
  12. limit = 1109496723125 # A002072(π(47)) = A002072(15)
  13. prime_nums = primes(smooth)
  14. smooth_prod = prod(prime_nums)
  15. function is_smooth_over_prod(n,k)
  16. g = gcd(n, k)
  17. while (g > 1)
  18. while (rem(n, g) == 0)
  19. n = div(n, g)
  20. end
  21. n == 1 && return true
  22. g = gcd(n, g)
  23. end
  24. n == 1
  25. end
  26. smooth_nums = Int64[1]
  27. for p in prime_nums
  28. for n in smooth_nums
  29. if (n*p <= limit)
  30. append!(smooth_nums, n*p)
  31. end
  32. end
  33. end
  34. total = 0
  35. for n in (smooth_nums)
  36. if (is_smooth_over_prod(n+1, smooth_prod))
  37. total += n
  38. end
  39. end
  40. return total
  41. end
  42. println(p_581())