mercredi 2 septembre 2015

Trouble using the sieve of eratosthenes

Given a number k, i have to find the smallest number which is not prime and does not divide with the first k prime numbers. I used the sieve of eratosthenes algorithm but i always get the result 1. I can`t understand why because i starts from 2. Any suggestions?

#define lim 100000
#include <fstream>
using namespace std;

bool prim [lim];
int i,j,k,c,n;

fstream f("prim.in");
ofstream g("prim.out");

int main()
{
f>>k;
for(i=2;i<=lim;i++)
{
if(prim[i]==0)
    {
        c++;
for(j=i*2;j<=lim;j+=i)
   prim[j]=1;
   }
if(prim[i]==0) if(c==k+1){ g << i*i; return 0;}
}
}

Aucun commentaire:

Enregistrer un commentaire