euler/euler031.rb

19 lines
513 B
Ruby
Raw Normal View History

2014-04-25 23:59:14 +00:00
def change(n, list_of_coins)
changeHelper(n, list_of_coins, [])
end
def changeHelper(n, list_of_coins, result_list)
if list_of_coins.empty?
[result_list]
elsif n == 0
[result_list]
else
if n >= list_of_coins.first
changeHelper(n - list_of_coins.first, list_of_coins, result_list << list_of_coins.first) +
changeHelper(n, list_of_coins[1..list_of_coins.count-1], result_list)
else
changeHelper(n, list_of_coins[1..list_of_coins.count-1], result_list)
end
end
end