Skip to main content
1 of 9
N. F.
  • 215
  • 3
  • 8

Please comment on this code for generating prime numbers within a range

I was given a problem to find out all the prime numbers within a range.Just after I wrote the program I found numerous other codes to solve the problem.I am interested to know is my algorithm efficient?is it like sieve filtration?(before writing this code I had no clear idea on sieve filtration method)or the algorithm I used is a crap.:P

#include<iostream.h>


void prime_gen(int a,int b)
{


    int array[4]={2,3,5,7},holder[1000000]={0},count=0,length=0;

    for(int i=a;i<b;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(i==array[j])
            {
                break;
            }
            else 
            {
                if(i%array[j]==0)
                {
                    count++;
                
                }
            }
        }
        if(count==0)
        {
            if(i>1)
            {
                holder[length++]=i;
            }
            else
            {
                length=0;
            }
        }
        count=0;
    }

    if(length==0)
    {   
        cout<<"No prime numbers exist within the range";
        cout<<"\n";
    
    }
    else 
    {
            cout<<"Prime numbers are:";
            for(int i=0;i<length;i++)
            {
                cout<<holder[i]<<"\t";
            }
        
            cout<<"Total count:"<<length<<endl;
        
    }


}

int main()
{

    int a,b;

    cout<<"Put the first integer:";
    cin>>a;

    cout<<"Put the second integer:";
    cin>>b;

    prime_gen(a,b); 

}
MiNdFrEaK
  • 215
  • 3
  • 8