Hello Everyone, I have been trying to solve the PRIME 1 problem.
I am able to get output perfectly but still on submission giving wrong answer.
Can you one please check.
#include<bits/stdc++.h>
using namespace std;
// Prime Generator
set v;
set::iterator it;
void populateMin()
{
v.insert(2);
v.insert(3);
v.insert(5);
v.insert(7);
v.insert(11);
}
void printPrimeNumbers(int f,int s,bool isPopulationTime)
{
int count = 0;
for(int i=f;i<=s;i++)
{
bool isPrime=true;
//if it is already in the populated prime number list, jst add to the count
if(v.find(i)==v.end()) //else check you need to populate.
{
// cout<<ânow i isâ<<i<<"\n";
if(v.empty())
{
populateMin();
}
for(it=v.begin();( it!=v.end() && i>(*it) );it++)
{
if(i%(*it)==0)
{
isPrime=false;
break;
}
}
if(isPrime)
{
it = v.end();
it--;
if(*it<i)
{
printPrimeNumbers(*it+1,i-1,true);
for(;it!=v.end();it++)
{
if(i%(*it)==0)
{
isPrime=false;
break;
}
}
}
}
}
if(isPrime){
if(i!=1&&!isPopulationTime){cout<<i<<"\n";}
v.insert(i);
}
}
}
struct Input
{
int f;
int s;
};
int main()
{
int n;
cin>>n;
Input *input;
input = new Input[n];
for(int i=0;i<n;i++)
{
int f,s;
cin>>f;
cin>>s;
input[i].f=f;
input[i].s=s;
}
cout<<"\n";
for(int i=0;i<n;i++)
{
printPrimeNumbers(input[i].f,input[i].s,false);
cout<<"\n";
}
return 0;
}