From 911d18bae56f44646b773dccb576b19d5f57f01a Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Fri, 14 May 2021 08:19:23 +0200 Subject: [PATCH] fix(curriculum): rework Project Euler 78 (#42104) * fix: rework challenge to use argument in function * fix: add solution * fix: improve look and spacing --- .../problem-78-coin-partitions.md | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-78-coin-partitions.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-78-coin-partitions.md index c5d96870e5..61f145116f 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-78-coin-partitions.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-78-coin-partitions.md @@ -8,7 +8,7 @@ dashedName: problem-78-coin-partitions # --description-- -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. +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$.
@@ -22,22 +22,40 @@ Let p(n) represent the number of different ways in which n coins can be separate | OO   O   O   O | | O   O   O   O   O | -
+
-Find the least value of `n` for which p(`n`) is divisible by one million. +Find the least value of `n` for which ${p}(n)$ is divisible by `divisor`. # --hints-- -`coinPartitions()` should return a number. +`coinPartitions(7)` should return a number. ```js -assert(typeof coinPartitions() === 'number'); +assert(typeof coinPartitions(7) === 'number'); ``` -`coinPartitions()` should return 55374. +`coinPartitions(7)` should return `5`. ```js -assert.strictEqual(coinPartitions(), 55374); +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); ``` # --seed-- @@ -45,16 +63,41 @@ assert.strictEqual(coinPartitions(), 55374); ## --seed-contents-- ```js -function coinPartitions() { +function coinPartitions(divisor) { return true; } -coinPartitions(); +coinPartitions(7); ``` # --solutions-- ```js -// solution required +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; +} ```