indexoutofboundsexception - Array index out of bounds in sieve of eratosthenes -
i having trouble arrayindexoutofbounds. new coding , don't understand problem , unable fix it. appreciated.
public class primes { public static void main(string[] args) { final int size = 10000; boolean[] numberisprime = new boolean[size]; int rowcounter = 0; for( int index = 1; index <= size; index++) { numberisprime[index] = true; } for( int index = 2; index <= size; index++) { if( numberisprime[index] = true){ for( int = index; <= size; i++){ numberisprime[index * i] = false; } } } for( int index = 1; index <= size; index++){ if( numberisprime[index] = true){ system.out.println(index + " "); rowcounter++; if( rowcounter == 10){ system.out.println(); } } } } }
you should tag question language you're using, i'm gonna assume it's java. problem is
boolean[] numberisprime = new boolean[size]; ... for( int index = 1; index <= size; index++) { numberisprime[index] = true; } the first line declares numberisprime array of size 10000. means can access numberisprime[0], numberisprime[1], ... numberisprime[9999]. can't access numberisprime[10000].
Comments
Post a Comment