2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f38b1000cf542c50fe9e
|
|
|
|
|
title: 'Problem 31: Coin sums'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 301965
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-31-coin-sums
|
2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --description--
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
<div style='margin-left: 4em;'>1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).</div>
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
It is possible to make £2 in the following way:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
<div style='margin-left: 4em;'>1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p</div>
|
|
|
|
|
|
|
|
|
|
How many different ways can `n` pence be made using any number of coins?
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`coinSums(50)` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof coinSums(50) === 'number');
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`coinSums(50)` should return 451.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(coinSums(50) == 451);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`coinSums(100)` should return 4563.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(coinSums(100) == 4563);
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`coinSums(150)` should return 21873.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-11-27 18:02:05 +00:00
|
|
|
|
assert(coinSums(150) == 21873);
|
|
|
|
|
```
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`coinSums(200)` should return 73682.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(coinSums(200) == 73682);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
function coinSums(n) {
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
return n;
|
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
coinSums(200);
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const coinSums = (n) => {
|
|
|
|
|
const getWays = (n, m=8, c=[1, 2, 5, 10, 20, 50, 100, 200]) => {
|
|
|
|
|
if (n === 0) return 1;
|
|
|
|
|
if (m === 0 || n < 0) return 0;
|
|
|
|
|
return getWays(n - c[m - 1], m, c) + getWays(n, m - 1, c);
|
|
|
|
|
};
|
|
|
|
|
return getWays(n);
|
|
|
|
|
};
|
|
|
|
|
```
|