Skip to content

Instantly share code, notes, and snippets.

@shieldsd
Created March 30, 2012 14:24
Show Gist options
  • Save shieldsd/2251883 to your computer and use it in GitHub Desktop.
Save shieldsd/2251883 to your computer and use it in GitHub Desktop.
Project Euler #31
amount = 200
coins = (200, 100, 50, 20, 10, 5, 2, 1)
ways = [1] + [0] * amount
# dynamic programing (quick)
for coin in coins:
for i in range(coin, amount + 1):
ways[i] += ways[i - coin]
print ways[amount]
# recursive (slower)
def ways(amount, coins):
if amount == 0:
return 1
elif len(coins) == 0:
return 0
else:
return sum((ways(amount - i * coins[0], coins[1:])
for i in range(amount / coins[0] + 1)))
print ways(amount, coins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment