12345678910111213141516171819202122232425262728 |
- #!/usr/bin/ruby
- # Author: Daniel "Trizen" Șuteu
- # License: GPLv3
- # Website: https://github.com/trizen
- # How many different ways can one hundred be written as a sum of at least two positive integers?
- # https://projecteuler.net/problem=76
- # Runtime: 0.626s
- var nums = []
- func count(n, i=0, sum=0) is cached {
- return 1 if (sum == n)
- return 0 if (sum > n)
- return 0 if (i > nums.end)
- count(n, i+1, sum) +
- count(n, i, sum + nums[i])
- }
- var n = 100
- nums = @(1 ..^ n)
- say count(100)
|