2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
id: 5900f3b91000cf542c50fecc
|
|
|
|
title: 'Problem 77: Prime summations'
|
2020-11-27 18:02:05 +00:00
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
forumTopicId: 302190
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: problem-77-prime-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 ten as the sum of primes in exactly five different ways:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
<div style='margin-left: 4em;'>
|
|
|
|
7 + 3<br>
|
|
|
|
5 + 5<br>
|
|
|
|
5 + 3 + 2<br>
|
|
|
|
3 + 3 + 2 + 2<br>
|
2021-05-14 06:19:58 +00:00
|
|
|
2 + 2 + 2 + 2 + 2<br><br>
|
2020-02-28 12:39:47 +00:00
|
|
|
</div>
|
|
|
|
|
2021-05-14 06:19:58 +00:00
|
|
|
What is the first value which can be written as the sum of primes in over `n` ways?
|
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:19:58 +00:00
|
|
|
`primeSummations(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:19:58 +00:00
|
|
|
assert(typeof primeSummations(5) === 'number');
|
2020-11-27 18:02:05 +00:00
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2021-05-14 06:19:58 +00:00
|
|
|
`primeSummations(5)` should return `11`.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
2021-05-14 06:19:58 +00:00
|
|
|
assert.strictEqual(primeSummations(5), 11);
|
|
|
|
```
|
|
|
|
|
|
|
|
`primeSummations(100)` should return `31`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(primeSummations(100), 31);
|
|
|
|
```
|
|
|
|
|
|
|
|
`primeSummations(1000)` should return `53`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(primeSummations(1000), 53);
|
|
|
|
```
|
|
|
|
|
|
|
|
`primeSummations(5000)` should return `71`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(primeSummations(5000), 71);
|
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:58 +00:00
|
|
|
function primeSummations(n) {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-14 06:19:58 +00:00
|
|
|
primeSummations(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:19:58 +00:00
|
|
|
function primeSummations(n) {
|
|
|
|
function getSievePrimes(max) {
|
|
|
|
const primesMap = new Array(max).fill(true);
|
|
|
|
primesMap[0] = false;
|
|
|
|
primesMap[1] = false;
|
|
|
|
const primes = [];
|
|
|
|
|
|
|
|
for (let i = 2; i < max; i += 2) {
|
|
|
|
if (primesMap[i]) {
|
|
|
|
primes.push(i);
|
|
|
|
for (let j = i * i; j < max; j += i) {
|
|
|
|
primesMap[j] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i === 2) {
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return primes;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MAX_NUMBER = 100;
|
|
|
|
const primes = getSievePrimes(MAX_NUMBER);
|
|
|
|
|
|
|
|
for (let curNumber = 2; curNumber < MAX_NUMBER; curNumber++) {
|
|
|
|
const combinations = new Array(curNumber + 1).fill(0);
|
|
|
|
combinations[0] = 1;
|
|
|
|
for (let i = 0; i < primes.length; i++) {
|
|
|
|
for (let j = primes[i]; j <= curNumber; j++) {
|
|
|
|
combinations[j] += combinations[j - primes[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (combinations[curNumber] > n) {
|
|
|
|
return curNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|