Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

82 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 5900f39e1000cf542c50feb1
title: 问题50连续的总和
challengeType: 5
videoUrl: ''
dashedName: problem-50-consecutive-prime-sum
---
# --description--
素数41可以写成六个连续素数的总和41 = 2 + 3 + 5 + 7 + 11 + 13这是连续素数的最长和它加到低于一百的素数。连续素数低于1000的连续素数加上一个素数包含21个项等于953.哪个素数低于一百万,可以写成最连续素数的总和?
# --hints--
`consecutivePrimeSum(1000)`应该返回953。
```js
assert.strictEqual(consecutivePrimeSum(1000), 953);
```
`consecutivePrimeSum(1000000)`应该返回997651。
```js
assert.strictEqual(consecutivePrimeSum(1000000), 997651);
```
# --seed--
## --seed-contents--
```js
function consecutivePrimeSum(limit) {
return true;
}
consecutivePrimeSum(1000000);
```
# --solutions--
```js
function consecutivePrimeSum(limit) {
function isPrime(num) {
if (num < 2) {
return false;
} else if (num === 2) {
return true;
}
const sqrtOfNum = Math.floor(num ** 0.5);
for (let i = 2; i <= sqrtOfNum + 1; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
function getPrimes(limit) {
const primes = [];
for (let i = 0; i <= limit; i++) {
if (isPrime(i)) primes.push(i);
}
return primes;
}
const primes = getPrimes(limit);
let primeSum = [...primes];
primeSum.reduce((acc, n, i) => {
primeSum[i] += acc;
return acc += n;
}, 0);
for (let j = primeSum.length - 1; j >= 0; j--) {
for (let i = 0; i < j; i++) {
const sum = primeSum[j] - primeSum[i];
if (sum > limit) break;
if (isPrime(sum) && primes.indexOf(sum) > -1) return sum;
}
}
}
```