SIeve optimizations

my Sieve code id working till 10000, here is the code


#include
using namespace std;
int n=10000;
void primesieve(int *p)
{
p[0]=p[1]=0;
p[2]=1;

for(int i=3;i<n;i+=2)
p[i]=1;

for(int i=3;i<n;i+=2)
{
if(p[i])
{
for(int j=ii;j<n;j+=(2i))
p[j]=0;
}
}
}
int main() {

//int N=100000;
int p[n]={0};

primesieve§;

int k,sum=0;
cin>>k;

for(int i=0;i<n;i++)
{
sum=sum+p[i];
if(sum==k)
{cout<<i;
break;}
}

return 0;
}


This is the code taught by deepak bhaiya , please tell me errors and modifications to be done to make it run for 10^6.

there is a video in our course optimization of prime sieve. i will highly recommended to watch that video

void primesieves’s first loop is not required. if it’s purpose is to intialize all the elements of seive with 1 then please see that you are incrementing it with 2 whereas it should be incremented with 1 only.

in second loop again you have to increment by 1 and NOT 2

in the nested loop you have to initialize j with i*2 and not i. this marks a prime number as composite.
for example if p[3] =1
then your code marks all its multiples with 2 and 3 itself as composite.

Also you have incremented it by 2*i whereas it has to be incremented by i so it reaches multiples of i.

Please do the necessary corrections and try again. It will surely work.