update,
This commit is contained in:
26
_resources/it114105/itp3914/Lab08/Ex5b.java
Normal file
26
_resources/it114105/itp3914/Lab08/Ex5b.java
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
public class Ex5b {
|
||||
public static void main(String [ ] args) {
|
||||
int times = 0;
|
||||
int count=1;
|
||||
System.out.print(2); // 2 is the first prime number
|
||||
int num=2;
|
||||
while (count < 20) {
|
||||
num++; // try next number
|
||||
boolean isPrime=true;
|
||||
for (int i=2; i<num; i++) {
|
||||
times++;
|
||||
if (num%i == 0 ){ // divisible by i
|
||||
isPrime = false; // not a prime
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPrime) {
|
||||
count++; // one more prime is found
|
||||
System.out.print(", " + num);
|
||||
}
|
||||
}
|
||||
System.out.println("\nDone");
|
||||
System.out.println("Total calculation = " + times);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user