To find prime numbers by filtering method

Source: Internet
Author: User
The number//screening method is: "Erato screening Method"//Find a non-prime to dig it off, the last is the prime number * * To find out the prime number of the 1~n * 1, Dig 1 * 2, with the next not to dig the numbers p remove the number of p, the multiples of p to dig out * 3, check P Is the integer part less than n (if n=1000, stick to p<31?), *    if it is, then return (2) to continue execution, otherwise end. * 4, the remainder is the prime number * * #include <iostream> #include <iomanip>using namespace std; #include <cmath>int main () { int I, J, N, a[101];for (i = 1; i <=; i++) {a[i] = i;} A[1] = 0;for (i = 2; I < sqrt (+); i++) {for (j = i + 1; J <=, J + +) {if (a[i]! = 0 && a[j]! = 0) {if (a [j]% a[i] = = 0) {A[j] = 0;}}} Cout<<endl;for (i = 1, n = 0; I <=; i++) {if (a[i]! = 0) {COUT<<SETW (5) <<a[i]<< ""; n++;} if (n = =) {cout<<endl;n = 0;}}    cout<<endl;    return 0;}

To find prime numbers by filtering method

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.