Class: Partitions
- Inherits:
-
Object
- Object
- Partitions
- Defined in:
- lib/partitions.rb
Instance Method Summary collapse
Instance Method Details
#enumerates(n) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/partitions.rb', line 3 def enumerates(n) partitions = {} partitions[1] = [[1]] 2.upto(n) do |value| partitions[value] = partition(value, partitions) end partitions[n] end |
#partition(n, previous) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/partitions.rb', line 12 def partition(n, previous) result = [[n]] (n - 1).downto(1) do |value| result.concat(sumfy(value, previous[n - value])) end result end |
#sumfy(number, partitions) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/partitions.rb', line 20 def sumfy(number, partitions) result = [] partitions.each do |partition| result << ([number] + partition) if (number >= partition.first) end result end |