1.4 KiB
1.4 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5900f3b81000cf542c50fecb | Problema 76: contando somme | 5 | 302189 | problem-76-counting-summations |
--description--
È possibile scrivere 5 come somma in esattamente sei modi diversi:
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
In quanti modi diversi puoi scrivere n
come somma di almeno due numeri interi positivi?
--hints--
countingSummations(5)
dovrebbe restituire un numero.
assert(typeof countingSummations(5) === 'number');
countingSummations(5)
dovrebbe restituire 6
.
assert.strictEqual(countingSummations(5), 6);
countingSummations(20)
dovrebbe restituire 626
.
assert.strictEqual(countingSummations(20), 626);
countingSummations(50)
dovrebbe restituire 204225
.
assert.strictEqual(countingSummations(50), 204225);
countingSummations(100)
dovrebbe restituire 190569291
.
assert.strictEqual(countingSummations(100), 190569291);
--seed--
--seed-contents--
function countingSummations(n) {
return true;
}
countingSummations(5);
--solutions--
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];
}