2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
id: 5900f3b81000cf542c50fecb
|
|
|
|
title: 'Problem 76: Counting summations'
|
2020-11-27 18:02:05 +00:00
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
forumTopicId: 302189
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: problem-76-counting-summations
|
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
|
|
|
It is possible to write five as a sum in exactly six different ways:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
<div style='margin-left: 4em;'>
|
|
|
|
4 + 1<br>
|
|
|
|
3 + 2<br>
|
|
|
|
3 + 1 + 1<br>
|
|
|
|
2 + 2 + 1<br>
|
|
|
|
2 + 1 + 1 + 1<br>
|
2021-05-14 06:18:52 +00:00
|
|
|
1 + 1 + 1 + 1 + 1<br><br>
|
2020-02-28 12:39:47 +00:00
|
|
|
</div>
|
|
|
|
|
2021-05-14 06:18:52 +00:00
|
|
|
How many different ways can `n` be written as a sum of at least two positive integers?
|
2020-02-28 12:39:47 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2021-05-14 06:18:52 +00:00
|
|
|
`countingSummations(5)` 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:18:52 +00:00
|
|
|
assert(typeof countingSummations(5) === 'number');
|
2020-11-27 18:02:05 +00:00
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2021-05-14 06:18:52 +00:00
|
|
|
`countingSummations(5)` should return `6`.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
2021-05-14 06:18:52 +00:00
|
|
|
assert.strictEqual(countingSummations(5), 6);
|
|
|
|
```
|
|
|
|
|
|
|
|
`countingSummations(20)` should return `626`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(countingSummations(20), 626);
|
|
|
|
```
|
|
|
|
|
|
|
|
`countingSummations(50)` should return `204225`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(countingSummations(50), 204225);
|
|
|
|
```
|
|
|
|
|
|
|
|
`countingSummations(100)` should return `190569291`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(countingSummations(100), 190569291);
|
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:18:52 +00:00
|
|
|
function countingSummations(n) {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-14 06:18:52 +00:00
|
|
|
countingSummations(5);
|
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:18:52 +00:00
|
|
|
function countingSummations(n) {
|
|
|
|
const combinations = new Array(n + 1).fill(0);
|
|
|
|
combinations[0] = 1;
|
|
|
|
|
|
|
|
for (let i = 1; i < n; i++) {
|
|
|
|
for (let j = i; j < n + 1; j++) {
|
|
|
|
combinations[j] += combinations[j - i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return combinations[n];
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|