-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumberMethod.java
More file actions
42 lines (37 loc) · 957 Bytes
/
PrimeNumberMethod.java
File metadata and controls
42 lines (37 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class PrimeNumberMethod {
public static void main(String[] args) {
System.out.println("The first 50 prime numbers are \n");
printPrimeNumbers(50);
}
public static void printPrimeNumbers(int numberOfPrimes) {
final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line
int count = 0;
int number = 2;
while (count < numberOfPrimes) {
if (isPrime(number)) {
count ++;
if (count % NUMBER_OF_PRIMES_LINE == 0) {
System.out.printf("%-5s\n", number);
}
else {
System.out.printf("%-5s", number);
}
}
number++;
}
}
public static boolean isPrime(int number) {
/*double division = number / 2;
for (int k = 2; k < division; k++) { // becase k is compare with division, k casting to double
if (number % k == 0) {
return false;
}
}*/
for (int division = 2; division <= number / 2; division++) {
if (number % division == 0) {
return false;
}
}
return true;
}
}