I have this code as an implementation of the 'Sieve of Eratosthenes'. But either it is super duper slow or it doesn't work. What is wrong with it?
p.s. I am finding all the primes numbers in a sorted array with numbers 1-100 inclusive.Code:public void sievePrimes() { numbers[0] = 0; int n = numbers[99]; for(int i=1; i<n; i++) { numbers[i] = 1; } int p = 2; while(p*p <= n) { int j = p*p; while(j <=n) { numbers[j-1] = 0; j = j+p; } while(numbers[p] != 1) { p++; } } for(int index=0; index < numbers.length; index++) { System.out.println(numbers[index]); } }




Reply With Quote