count_partitions.jl 676 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/julia
  2. # Trizen
  3. # Date: 18 August 2016
  4. # https://github.com/trizen
  5. # Count the number of partitions of n.
  6. # See also: https://oeis.org/A000041
  7. # https://en.wikipedia.org/wiki/Partition_(number_theory)
  8. function count_partitions(x::Int64)
  9. n = 2
  10. p = Int64[1]
  11. while (n <= x+1)
  12. i = 0
  13. q = 2
  14. push!(p, 0)
  15. while q <= n
  16. p[n] += (i % 4 > 1 ? -1 : 1) * p[n-q+1]
  17. i += 1
  18. j = div(i, 2) + 1
  19. isodd(i) && (j *= -1)
  20. q = div(j * (3j - 1), 2) + 1
  21. end
  22. n += 1
  23. end
  24. p[n-1]
  25. end
  26. println("P(200) = ", count_partitions(200)) # 3972999029388