076 Counting summations.sf 504 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # How many different ways can one hundred be written as a sum of at least two positive integers?
  6. # https://projecteuler.net/problem=76
  7. # Runtime: 0.626s
  8. var nums = []
  9. func count(n, i=0, sum=0) is cached {
  10. return 1 if (sum == n)
  11. return 0 if (sum > n)
  12. return 0 if (i > nums.end)
  13. count(n, i+1, sum) +
  14. count(n, i, sum + nums[i])
  15. }
  16. var n = 100
  17. nums = @(1 ..^ n)
  18. say count(100)