2018-09-30 22:01:58 +00:00
---
id: 5900f3ba1000cf542c50fecd
title: 'Problem 78: Coin partitions'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302191
2021-01-13 02:31:00 +00:00
dashedName: problem-78-coin-partitions
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
2021-05-14 06:19:23 +00:00
Let ${p}(n)$ represent the number of different ways in which `n` coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so ${p}(5) = 7$.
2018-09-30 22:01:58 +00:00
2020-02-07 08:47:35 +00:00
< div style = 'text-align: center;' >
2020-11-27 18:02:05 +00:00
| Coin piles |
| ----------------- |
| OOOOO |
| OOOO O |
| OOO OO |
| OOO O O |
| OO OO O |
| OO O O O |
| O O O O O |
2020-02-07 08:47:35 +00:00
2021-05-14 06:19:23 +00:00
< / div > < br >
2020-02-07 08:47:35 +00:00
2021-05-14 06:19:23 +00:00
Find the least value of `n` for which ${p}(n)$ is divisible by `divisor` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-05-14 06:19:23 +00:00
`coinPartitions(7)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-14 06:19:23 +00:00
assert(typeof coinPartitions(7) === 'number');
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-05-14 06:19:23 +00:00
`coinPartitions(7)` should return `5` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-14 06:19:23 +00:00
assert.strictEqual(coinPartitions(7), 5);
```
`coinPartitions(10000)` should return `599` .
```js
assert.strictEqual(coinPartitions(10000), 599);
```
`coinPartitions(100000)` should return `11224` .
```js
assert.strictEqual(coinPartitions(100000), 11224);
```
`coinPartitions(1000000)` should return `55374` .
```js
assert.strictEqual(coinPartitions(1000000), 55374);
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
```js
2021-05-14 06:19:23 +00:00
function coinPartitions(divisor) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-05-14 06:19:23 +00:00
coinPartitions(7);
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
2021-05-14 06:19:23 +00:00
function coinPartitions(divisor) {
const partitions = [1];
let n = 0;
while (partitions[n] !== 0) {
n++;
partitions.push(0);
let i = 0;
let pentagonal = 1;
while (pentagonal < = n) {
const sign = i % 4 > 1 ? -1 : 1;
partitions[n] += sign * partitions[n - pentagonal];
partitions[n] = partitions[n] % divisor;
i++;
let k = Math.floor(i / 2) + 1;
if (i % 2 !== 0) {
k *= -1;
}
pentagonal = Math.floor((k * (3 * k - 1)) / 2);
}
}
return n;
}
2018-09-30 22:01:58 +00:00
```