java - Sieve of Eratosthenes implementation not checking certain numbers -


i writing program find prime numbers using ol' sieve of eratosthenes algorithm (although twist). in order halve size of byte-array needed, don't represent numbers, , stick odd (calculating position in array integer division two).

i have 1 problem, however. program doesn't seem recognize 17 or 113 prime numbers, although indeed prime numbers. here code:

public class eratosthes {      byte[] checklist;     int range;      public eratosthesconcurrent(int range) {         this.range = range/2;         this.checklist = new byte[this.range];         this.initallchecked();         this.findprimesseq();         this.printprimes();     }      private void initallchecked(){         for(int = 1; < checklist.length; i++){             checklist[i] = 1;         }     }      private void crossout(int i){         checklist[i/2] = 0;     }      private void findprimesseq(){         int curr = 3;         outer: while(curr < range*2){             for(int = curr*curr; < range*2; i+=2*curr){                 if(i != curr && i%curr == 0){                     if(i == 113){                         system.out.println(curr);                     }                     crossout(i);                 }             }              secondloop: for(int = curr; < range*2; i++){                 if(checklist[i/2] == 1 && curr != i){                     curr = i;                     break secondloop;                 } else if(i == range*2-1 || == range*2-2){ //should changed, might miss last prime                     break outer;                 }             }         }     }      public void printprimes(){         for(int = 1; < range; i++){             if(checklist[i] == 1){                 system.out.println("prime found: " + ((i*2)+1));             }         }     } } 

adding following crossout method not produce output:

if(i == 17 || == 113){     system.out.println("found 1 of numbers"); } 

and that's strange me, because printout program excludes 17 , 113, , not check of multiples. leaves me array of bytes primes checked... multiple of 17 or 113.

i'd happy if find error in program -- i've been debugging hours myself, no result. in advance.

the problem appears you're not checking division 2.

you're calling crossout(16) @ point (specifically multiple of 4), crosses out corresponding value 17. 112 causing 113 crossed out.

change:

if (i != curr && % curr == 0) { 

to:

if (i % 2 != 0 && != curr && % curr == 0) { 

and both of gets printed prime.

also, i'm bit concerned loop doing - didn't @ output in detail, although appears primes, imagine should looping on multiples of curr, first being 2*curr (not curr*curr), increment between being curr (not 2*curr), although i'm not saying you've done incorrect, need bit more time understand how code solving problem.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -