Skip to content

Instantly share code, notes, and snippets.

@shayarnett
Created October 9, 2009 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shayarnett/206210 to your computer and use it in GitHub Desktop.
Save shayarnett/206210 to your computer and use it in GitHub Desktop.
def count_change(amount, denominations = [50,25,10,5,1])
result = 1
largest_denomination_count = 1
until denominations.size == 1
until makes_change?(amount, denominations, largest_denomination_count) == false
puts "change made with #{largest_denomination_count} coins of value #{denominations.first}"
result += 1
largest_denomination_count += 1
end
largest_denomination_count = 1
denominations.slice!(0)
end
puts "#{result} possible combinations for change of #{amount}"
end
def makes_change?(amount, denominations, largest_denomination_count)
(denominations.first * largest_denomination_count) <= amount
end
count_change(100)
# problem is this only counts 1 combination as it decrements :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment