2018-10-10 18:03:03 -04:00
---
id: a3bfc1673c0526e06d3ac698
title: Sum All Primes
isRequired: true
challengeType: 5
2019-08-28 16:26:13 +03:00
forumTopicId: 16085
2018-10-10 18:03:03 -04:00
localeTitle: Сумма всех чисел
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
2020-06-30 01:51:26 -07:00
Суммируйте все простые числа до и включив предоставленный номер. Простое число определяется как число больше единицы и имеет только два делителя: один и сам. Например, 2 - простое число, потому что оно делится только на одно и два. Предоставленный номер не может быть простым. Н е забудьте использовать < a href = "https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target = "_blank" > Read-Search-Ask,< / a > если вы застряли. Попробуйте подключить программу. Напишите свой собственный код.
2019-08-28 16:26:13 +03:00
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > sumPrimes(10)</ code > should return a number.
testString: assert.deepEqual(typeof sumPrimes(10), 'number');
- text: < code > sumPrimes(10)</ code > should return 17.
testString: assert.deepEqual(sumPrimes(10), 17);
- text: < code > sumPrimes(977)</ code > should return 73156.
testString: assert.deepEqual(sumPrimes(977), 73156);
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function sumPrimes(num) {
return num;
}
sumPrimes(10);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function eratosthenesArray(n) {
var primes = [];
if (n > 2) {
var half = n>>1;
var sieve = Array(half);
for (var i = 1, limit = Math.sqrt(n)>>1; i < = limit; i++) {
if (!sieve[i]) {
for (var step = 2*i+1, j = (step*step)>>1; j < half ; j + = step ) {
sieve[j] = true;
}
}
}
primes.push(2);
for (var p = 1; p < half ; p + + ) {
if (!sieve[p]) primes.push(2*p+1);
}
}
return primes;
}
function sumPrimes(num) {
return eratosthenesArray(num+1).reduce(function(a,b) {return a+b;}, 0);
}
sumPrimes(10);
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >