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 bad?
#include<iostream.h>
#include<math.h>
prime_gen
void prime_gen(int a,int b)
{
int array[4]={2,3,5,7},holder[1000000]={0},count=0,flag=0,length=0,number_count=0;
for(int i=0;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++)
{
int t=holder[i];
for(int check=0;check<sqrt(length);check++)
{
if(t!=holder[check])
{
if(t%holder[check]==0)
{
t=0;
}
}
}
if(t!=0&&t>=a)
{
number_count++;
cout<<t<<"\t";
}
}
cout<<"Total count:"<<number_count<<endl;
}
}
main
int main()
{
int a,b;
cout<<"Put the first integer:";
cin>>a;
cout<<"Put the second integer:";
cin>>b;
prime_gen(a,b);
}