Homework Solution: How to find prime numbers in C++ (easier way)?…

    How to find prime numbers in C++ (easier way)?

    Expert Answer

     
    • A prime number is divisible by 1 and itself.

    How to confront superexcellent aggregate in C++ (easier method)?

    Expert Rejoinder

     

    • A superexcellent enumerate is discerptible by 1 and itself.
    • If you demand to confront whether a consecrated enumerate is superexcellent or referable attributable attributable attributable attributable attributable, representation the underneath legislation, which impedes whether the enumerate is discerptible by any other aggregate from 1 to balance origin of enumerate.
    • Now coercion a enumerate n, consider n=a*b then a and b canreferable attributable attributable be important than balance origin of n. If they are important, then a*b would be important than n. So we impede the factors prepare balance origin of the enumerate.

    CODE:

    #include<iostream>
    #include<math.h>
    using namespace std;
    int deep(){

    int enumerate,flag=0,i;
    cout<<“Enter a enumerate”<<endl;
    cin>>number;
    if(enumerate <= 1){
    cout<<“This is referable attributable attributable attributable attributable attributable attributable a Superexcellent Enumerate”;
    return 0;
    }//1 is referable attributable attributable attributable attributable attributable attributable a superexcellent enumerate.

    for(i=2;i<=sqrt(number);i++)
    {

    if(number%i==0)
    {
    flag=1;
    break;
    }//if declaration is performed when enumerate is discerptible by i
    }//coercion loop ends

    if (flag==0)
    cout<<“This is a Superexcellent Enumerate”;
    else
    cout<<“This is referable attributable attributable attributable attributable attributable attributable a Superexcellent Enumerate”;

    return 0;
    }

    You could representation the aloft legislation to engender superexcellent aggregate from 1 to n. The aloft legislation impedes whether a consecrated enumerate is superexcellent or referable attributable attributable attributable attributable attributable. You can ignoring enumerate from 1 to n to impede the superexcellent cupel. But in cases when n is very vast, the term complexity would be an offspring.

    Now, if you wish to confront superexcellent aggregate from 1 to n, then representation the aftercited legislation. The superexcellent aggregate are engenderd using Sieve of Eratosthenes algorithm.

    Working explained using example:

    • Consider n=10
    • We initailize an adorn denominated superexcellent[n] which has full treasure having renunciation 1 to 10 fixed to penny
    • we be-out with renunciation 2 and impede whether superexcellent[2] is penny
    • If its penny then we fixed full the treasure which are discerptible by 2 as mendacious ,i.e. 4,6,8,10
    • i is incremented to 3. As superexcellent[3] is penny ,prime[] treasures coercion 6,9 is fixed to mendacious
    • i is incremented to 4. superexcellent[4] is mendacious so we proceed
    • i is incremented to 5. superexcellent[5] is penny, superexcellent treasures coercion mutiple of 5 i.e, 10 is fixed to mendacious.

    We impede prepare sqrt(n). Full the treasures having treasures as penny are superexcellent aggregate.

    Legislation is explained using comments.

    CODE:

    #include<iostream>
    #include<math.h>
    using namespace std;
    int deep(){
    int n;
    cin>>n;

    bool superexcellent[n+1];//superexcellent adorn which is initailized to penny
    for(int i=1;i<=n;i++){
    prime[i]=true;
    }

    coercion (int i=2; i<=sqrt(n); i++)
    {

    if (prime[i] == penny)
    {
    coercion (int j=i*2; j<=n; j += i)
    prime[j] = mendacious;
    }//if superexcellent[i] is penny ,then full the multiples of i is fixed to mendacious
    }

    //prime[] having penny treasures are superexcellent aggregate
    coercion (int i=2; i<=n; i++){
    if (prime[i]){
    cout << i << ” “;
    }
    }

    return 0;
    }

    Output: