# 找到250以下的素数之和(finding sum of prime numbers under 250)

229 IT屋
```var sum = 0

for (i = 0; i < 250; i++) {

function checkIfPrime() {

for (factor = 2; factor < i; factor++) {
if (i % factor = 0) {
sum = sum;
}
else {
sum += factor;
}
}
}
}

document.write(sum);
```

I am trying to check for the sum of all the prime numbers under 250. I am getting an error saying that i is invalid in the statement if (i % factor = 0) I know was creating in the original for statement, but is there any way to reference it in the if statement?

With the prime computation, have you considered using Sieve of Eratosthenes? This is a much more elegant way of determining primes, and, summing the result is simple.

```var sieve = new Array();
var maxcount = 250;
var maxsieve = 10000;

// Build the Sieve, marking all numbers as possible prime.
for (var i = 2; i < maxsieve; i++)
sieve[i] = 1;

// Use the Sieve to find primes and count them as they are found.
var primes = [ ];
var sum = 0;
for (var prime = 2; prime < maxsieve && primes.length < maxcount; prime++)
{
if (!sieve[prime]) continue;
primes.push(prime); // found a prime, save it
sum += prime;
for (var i = prime * 2; i < maxsieve; i += prime)
sieve[i] = 0; // mark all multiples as non prime
}

document.getElementById("result").value =
"primes: " + primes.join(" ") + "\n"
+ "count: " + primes.length + "\n"
+ "sum: " + sum + "\n";```
```#result {
width:100%;
height:180px
}```
```<textarea id="result">
</textarea>```

(EDIT) With the updated algorithm, there are now two max involved:

• maxcount is the maximum number of prime numbers you wish to find
• maxsieve is a guess of sieve large enough to contain maxcount primes

You will have to validate this by actually checking the real count since there are two terminating conditions (1) we hit the limit of our sieve and cannot find any more primes, or (2) we actually found what we're looking for.

If you were to increase the number to numbers much greater than 250, than the Sieve no longer becomes viable as it would be consume great deals of memory. Anyhow, I think this all makes sense right? You really need to play with the Sieve yourself at this point than rely on my interpretation of it.

`  var sum = 0   for（i = 0; i< 250; i ++）{  function checkIfPrime（） {  for（factor = 2; factor< i; factor ++）{ if（i％factor = 0）{ sum = sum; }  else { sum + = factor; } } } }   document.write（sum）;   `

`  var sieve = new Array（）; var maxcount = 250; var maxsieve = 10000; //构建Sieve，将所有数字标记为prime.for（var i = 2; i< maxsieve; i ++）sieve [i] = 1; //使用Sieve查找素数并在找到它们时对它们进行计数.var primes = []; var sum = 0; for（var prime = 2; prime< maxsieve&& primes。长度< maxcount; prime ++）{if（！sieve [prime]）继续; primes.push（总理）; //找到一个素数，保存总和+ =素数; for（var i = prime * 2; i< maxsieve; i + = prime）sieve [i] = 0; //将所有倍数标记为非素数} document.getElementById（result）。value =primes：+ primes.join（）+\ n+count：+ primes.length +\\ \\ n+总和：+ sum +\ n;  `
` < code> #result {width：100％;身高：180px}  `
` < textarea id =result >< / textarea>  `

（编辑）随着更新算法，现在有两个最大参与：

• maxcount 是您希望找到的素数的最大数量

• maxsieve 是对筛子的猜测，其大小足以包含 maxcount primes