I am trying to solve the Prime1 generator problem but I am getting a runtime error.
Can anyone please help me with this.
On testing it on the range 100000, 1000000 on ideone.com it gives a runtime error
Here is my code:
#include<iostream>
#include<math.h>
using namespace std;
class isPrimeClass
{
public:
int n,m,L;
bool *isPrimeBoolArr;
isPrimeClass(){}
init(int n,int m,int L)
{
this->n=n;
this->m=m;
this->L=L;
isPrimeBoolArr=new bool[m-n+1];
for(int i=0;i<=m-n;i++)
{
isPrimeBoolArr[i]=true;
}
}
};
bool checkIfPrime(int n)
{
int counter=0;
if(n==0||n==1)
{
return false;
}
else if(n==2)
{
return true;
}
else
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
counter++;
}
}
if(counter==0)
{
return true;
}
else
{
return false;
}
}
}
main()
{
int L,k;
cin>>L;
int M,N;
int array[L][2];
isPrimeClass isPrimeClassArr[L];
for(int i=0;i<L;i++)
{
cin>>array[i][0]>>array[i][1];
isPrimeClassArr[i].init(array[i][0],array[i][1],L);
}
for(int h=0;h<L;h++)
{
M=array[h][1];
N=array[h][0];
for(int i=2;i<=sqrt(M);i++)
{
k=(N/i)*i;
for(int j=k;j+i<=M;j=j+i)
{
isPrimeClassArr[h].isPrimeBoolArr[j-N]=false;
}
}
}
for(int h=0;h<L;h++)
{
M=array[h][1];
N=array[h][0];
for(int i=2;i<=sqrt(M);i++)
{
if(checkIfPrime(i))
{
isPrimeClassArr[h].isPrimeBoolArr[i-N]=true;
}
}
if(!checkIfPrime(M))
isPrimeClassArr[h].isPrimeBoolArr[M-N]=false;
if(!checkIfPrime(M-1))
isPrimeClassArr[h].isPrimeBoolArr[M-N-1]=false;
if(N==1)
{
isPrimeClassArr[h].isPrimeBoolArr[0]=false;
}
}
for(int h=0;h<L;h++)
{
M=array[h][1];
N=array[h][0];
for(int i=0;i<=M-N;i++)
{
if(isPrimeClassArr[h].isPrimeBoolArr[i])
{
cout<<i+N<<" ";
}
}
cout<<"\n";
}
}