Hi All,
I have submitted solution to Prime Generator Problem. I have run it in Visual Studio and gives me correct output for small or large numbers.
Still I am getting wrong answer when I submit the code:
//2. Prime Generator
//Problem code: PRIME1
#include "iostream"
#include <stdio.h>
#include <math.h>
void find_prime_fast(unsigned long m, unsigned long n)
{
unsigned long *arr = new(std::nothrow) unsigned long[n+1];
if(arr==NULL)
{
std::cout<<"Memory could not be allocated";
delete [] arr;
return;
}
for (unsigned long i=0;i<n;i++)
{
*(arr+i)=i;
}
unsigned long a=sqrt((long double)n);
unsigned long x,y,z;
for(unsigned long i=2;i<=a;i++)
{
for(int b=0; ; b++)
{
x= i*i; y= i*b;// Result of i^2,i^2+i,i^2+2i is serached in array with its index and replaced with 0 to denote that its not prime.
z=x+y;
if(z<n)
arr[z]=0;
else
break;
}
}
for(long i=m;i<n;i++)
{
if(arr[i]!=0)
std::cout<<"\n"<<arr[i];
}
delete [] arr;
}
int main()
{
std::cout<<"Enter the value for no of testcases t (t<=10)\n";
int t;
std::cin>>t;
if(t>10) { std::cout<<"\nNo more than 10 testcases please.\n"; return 1;}
unsigned long m,n;
for(int i=1;i<=t;i++)
{
std::cout<<"Enter the range for testcase no "<< i << "\n";
std::cin>>m;
std::cin>>n;
if (m<1 || n<1 || n < m || (n-m) >100000) { std::cout<<"\n Enter two numbers m and n such that 1<= m <= n <= 1000000000, n-m<=100000.\n"; return 1; }
find_prime_fast(m,n);
std::cout<<"\n";
}
return 0;
}
Can you help?