""" projecteuler.net problem 31 : In England the currency is made up of pound and pence with 1 pound = 100 pence. There are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, 1 pound (100p) and 2 pound (200p). One way to make 2 pounds with these coins is 1 100p + 1 50p + 2 20p + 1 5p + 1 2p + 3 1p How many different ways can 2 pounds be made using any number of coins? Jim M | GPL | Nov 2010 """ # index: 0 1 2 3 4 5 6 7 coins = [200, 100, 50, 20, 10, 5, 2, 1] last_index = len(coins) - 1 def count_ways(amount_left, index): """Recursively calculate number of ways to make change. >>> count_ways(4, 7) # 4p with [1p] coins: (4) 1 >>> count_ways(4, 6) # 4p with [2p, 1p] coins: (2,0),(1,2),(0,4) 3 >>> count_ways(200, 0) # answer to problem 31 73682 """ if index == last_index: return 1 # 1 way : use amount_left of 1p coins else: coin_value = coins[index] ways = 0 for n in range(1 + amount_left/coin_value): result = count_ways(amount_left - n*coin_value, index + 1) ways += result return ways def main(): print "Number of ways to get 200 pound = %i" % count_ways(200, 0) if __name__ == "__main__": import doctest doctest.testmod() main()