Skip to content

Commit 683474c

Browse files
Hyftarharshildarji
authored andcommitted
Add Problem 31 solution
1 parent 9097977 commit 683474c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Project Euler/Problem 31/sol1.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import print_function
3+
try:
4+
raw_input # Python 2
5+
except NameError:
6+
raw_input = input # Python 3
7+
'''
8+
Coin sums
9+
Problem 31
10+
In England the currency is made up of pound, £, and pence, p, and there are
11+
eight coins in general circulation:
12+
13+
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
14+
It is possible to make £2 in the following way:
15+
16+
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
17+
How many different ways can £2 be made using any number of coins?
18+
'''
19+
20+
21+
def one_pence():
22+
return 1
23+
24+
25+
def two_pence(x):
26+
return 0 if x < 0 else two_pence(x - 2) + one_pence()
27+
28+
29+
def five_pence(x):
30+
return 0 if x < 0 else five_pence(x - 5) + two_pence(x)
31+
32+
33+
def ten_pence(x):
34+
return 0 if x < 0 else ten_pence(x - 10) + five_pence(x)
35+
36+
37+
def twenty_pence(x):
38+
return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x)
39+
40+
41+
def fifty_pence(x):
42+
return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x)
43+
44+
45+
def one_pound(x):
46+
return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x)
47+
48+
49+
def two_pound(x):
50+
return 0 if x < 0 else two_pound(x - 200) + one_pound(x)
51+
52+
53+
print(two_pound(200))

0 commit comments

Comments
 (0)