Solution to problem 31
This commit is contained in:
parent
2cb8ed1637
commit
d4e37d8e41
43
src/Python/Problem031.py
Normal file
43
src/Python/Problem031.py
Normal file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Created on 24 Feb 2021
|
||||
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for problem 31 of Project Euler
|
||||
https://projecteuler.net/problem=31
|
||||
"""
|
||||
|
||||
from itertools import product
|
||||
from utils import timeit
|
||||
|
||||
|
||||
@timeit("Problem 31")
|
||||
def compute():
|
||||
"""
|
||||
In the United Kingdom the currency is made up of pound (£) and pence (p).
|
||||
There are eight coins in general circulation:
|
||||
|
||||
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).
|
||||
|
||||
It is possible to make £2 in the following way:
|
||||
|
||||
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
|
||||
|
||||
How many different ways can £2 be made using any number of coins?
|
||||
"""
|
||||
no_ways = 0
|
||||
|
||||
coins = [2, 5, 10, 20, 50, 100]
|
||||
|
||||
bunch_of_coins = product(*[range(0, 201, i) for i in coins])
|
||||
for money in bunch_of_coins:
|
||||
if sum(money) <= 200:
|
||||
no_ways += 1
|
||||
# consider also the case for 200 coins of 1p
|
||||
return no_ways + 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem 31: {compute()}")
|
Loading…
x
Reference in New Issue
Block a user