Outputing prime numbers- Java -
this question has answer here:
- prime numbers in java - algorithms 6 answers
i'm having trouble code. i'm trying write method output prime numbers 2-10,000. i'm still beginner in java , wasn't sure how go doing this, know use binary search method , loops this. tried follow examples reading through in textbook , online; came with, not working properly. i'm not sure if it's entirely correct. or advice on how go doing or fixing appreciated.
public static void prime() { int i; // variable loop for(i=2; i<=10000; i++) { int factors =0; int j = 1; while(j<=i) { if(i%j == 0) { factors++; } //end if j++; } if(factors == 2) { system.out.println(i); } //end if }// end } // end method prime
think prime means: number divisible 1 , itself. using definition, can write more efficient piece of code checks whether current number evenly divisible number other 1 , itself, i.e 2.
Comments
Post a Comment