java - Can't see where I'm dividing by 0? -
here code think need able asses problem
1 import.java.util.scanner 2 public class ccattano_sieve{ 3 private boolean [] primes = new boolean [50001]; 4 private int upper; 5 private int lower; 6 7 public ccattano_sieve(){ 8 upper = 50000; 9 lower = 1; 10 (int = 2; < primes.length; i++){ 11 primes[i] = true; 12 } 13 primes[0] = false; 14 primes[1] = false; 15 } 16 17 public void processsieve(){ 18 (int = 2; < math.round(math.sqrt(50000)); i++){ 19 if (primes[i] == true){ 20 (int c = 2; c < (primes.length - 1); i++){ 21 if (c % == 0){ 22 primes[c] = false; 23 } 24 else{ 25 primes[c] = true; 26 } 27 } 28 } 29 } 30 }
i'm pretty sure else statement on lines 24 - 26 aren't needed added when trying trouble shoot. on line 21 when trying run code receive divide 0 error. exact error follows.
exception in thread "main" java.lang.arithmeticexception: / 0 @ ccattano_sieve.processsieve(ccattano_sieve.java:21) @ ccattano_sievetest.main(ccattano_sievetest.java:7)
this line "at ccattano_sievetest.main(ccattano_sievetest.java:7)" calls code pasted can ignored. line 21 main issue , can't find solution.
the modulus operator "rest of division" meaning involves division.
i believe have bug on line 20 incrementing instead of c. means variable overflow (reach high turn negative) , turn 0.
Comments
Post a Comment