Deepak and prime

why is this exceeding a time limit ??

#include
#include
using namespace std;
typedef long long ll;
int main() {

bool a[5000000];
ll n;
cin>>n;
memset(a,1,sizeof(a));
for(ll p=2;p*p<=n;p++){
	if(a[p]==true){
		for(ll i=p*p;p<n;i+=p){
			a[p]=false;
		}
	}
}
for(ll i=2;i<n;i++){
	if(a[i]==true){
		cout<<i<<" ";
	}
}


return 0;

}

@neelmani98
hello neelmani,
a) u only need to print nth prime number and not n primes
b) because 5000000th prime number can be greater than 5000 000 , u need to increase ur sieve size.

can you please suggest me a solution for this i tried changing the sieve size but now i am getting run time error

@neelmani98
ur code is already correct.
a) first check what is 5000 000 th prime number(from internet) then declare ur sieve array of size bigger than that prime
b)

in this snippet change n with the value of 5000 000th prime number

c) now print nth prime number

#include #include using namespace std; typedef long long ll; int main() { ll a[7368787]; ll res[7368787]; ll n; int i=1; cin>>n; memset(a,1,sizeof(a)); for(ll p=2;pp<=7368787;p++){ if(a[p]==1){ for(ll i=pp;p<7368787;i+=p){ a[p]=0; } } } for(ll p=2;p<7368787;p++) if(a[p]==1) { res[i]=p; i++; } cout<<res[n]; return 0; }

@neelmani98

5000 000th prime number bro not 500 000th prime number

changed to 5000000th prime number still there is some error in code please look to that

@neelmani98
ok,
so first save ur code on https://ide.codingblocks.com/ and share the link

@neelmani98 hey try segmented sieve in this question.

@rishabhmahajan546
normal sieve will also do the work

link to my saved code https://ide.codingblocks.com/s/197488

pls refer this-> https://ide.codingblocks.com/s/197508

dont know why ur coding giving 0 all timr