Sum All Primes
Sum of prime numbers less than or equal to a given number.
Only 1 and its own two approximate numbers are called prime numbers. For example, 2 is a prime number, because it can only be divisible by 1 and 2. 1 is not a prime number because it can only be divisible by itself.
The given number is not necessarily prime.
For Loops
Array.push ()
Ideas:
Find all the prime numbers from 0 to num, and then iterate through the accumulation;
Knowledge Points:
Prime numbers are not divisible by themselves (not included) except 2;
Mistakes:
I put it to make sure that a is divisible by the B in the global, which causes a each loop but the value of B does not change, then I declare B to a loop;
Code:
1 functionsumprimes (num) {2 vararr = [2];3 varIDX = 0;4 for(varA = 3; a <= num; a++) {5 varb = 0;6 for(vari = 2; i < A; i++) {7 if(a% i = = 0) {8b = 1;9 Break;Ten } One } A if(b = = 0) { - Arr.push (a); - } the } - - for(varj = 0; J < Arr.length; J + +) { -IDX + =Arr[j]; + } - returnidx; + } A atSumprimes (10);
The sum of all primes in the FCC intermediate arithmetic problem