PyPE solution to PE problem 31
PyPE solution to PE problem 31
Blueprint information
- Status:
- Complete
- Approver:
- None
- Priority:
- Undefined
- Drafter:
- None
- Direction:
- Needs approval
- Assignee:
- None
- Definition:
- Approved
- Series goal:
- None
- Implementation:
- Implemented
- Milestone target:
- None
- Started by
- Scott Armitage
- Completed by
- Scott Armitage
Related branches
Related bugs
Sprints
Whiteboard
ProjectEule
===
In England the currency is made up of pound, L, and pence, p, and there
are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, L1 (100p) and L2 (200p).
It is possible to make L2 in the following way:
1*L1 + 1*50p + 2*20p + 1*5p + 1*2p + 3*1p
How many different ways can L2 be made using any number of coins?
Solution
--------
There are two solutions presented here: the first is a hard-coded solution
for the specific problem given, which is much faster (~5ms); the second is
a general solution that solves any problem where we are given a list of
values and we wish to know how many combinations of those values will add
up to some desired value.
The first solution simply iterates over all possible combinations, with a
few optimizations. First, we don't iterate over the number of 1p pieces we
use, as there will only ever be exactly one valid candidate given the
number of other coins. Second, we only iterate each given coin in the
possible range at each step.
The second solution is a recursive algorithm that removes the largest value
from the list of values, and iterates over all possible multiples of that
value, adding the number of combinations of the remaining problem. The stop
conditions of the recursion are when the desired value is 0 or the list of
available values is empty.
The recursive algorithm is significantly slower (~50x).
Answer
------
73682