The sum of all primes in the FCC intermediate arithmetic problem

Source: Internet
Author: User
Tags arithmetic

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.