078 Coin partitions -- rec.jl 941 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/julia
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 19 August 2016
  4. # License: GPLv3
  5. # Website: https://github.com/trizen
  6. # https://projecteuler.net/problem=78
  7. # Runtime: 1.526s
  8. function partitions_count(n::Int64, mod::Int64, cache)
  9. if haskey(cache, n)
  10. return cache[n]
  11. end
  12. n <= 1 && return n
  13. sum_1 = 0
  14. for i in 1:Int64(floor((sqrt(24*n + 1) + 1)/6))
  15. sum_1 += ((-1)^(i-1) * partitions_count(n - div(i*(3*i - 1), 2), mod, cache)) % mod
  16. end
  17. sum_2 = 0
  18. for i in 1:Int64(ceil((sqrt(24*n + 1) - 7)/6))
  19. sum_2 += ((-1)^(i-1) * partitions_count(n - div((-i) * (-3*i - 1), 2), mod, cache)) % mod
  20. end
  21. x = (sum_1 + sum_2) % mod
  22. cache[n] = x
  23. x
  24. end
  25. function p078()
  26. n = 1
  27. cache = Dict{Int64, Int64}()
  28. while true
  29. if partitions_count(n, 1_000_000, cache) == 0
  30. println(n-1)
  31. break
  32. end
  33. n += 1
  34. end
  35. end
  36. p078()