forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise07_06.java
47 lines (45 loc) · 2 KB
/
Exercise07_06.java
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
43
44
45
46
47
package ch_07;
import java.util.*;
/**
* *7.6 (Revise Listing 5.15, PrimeNumber.java)
* Listing 5.15 determines whether a number n is prime by checking whether 2, 3, 4, 5, 6, ..., n/2 is a divisor.
* If a divisor is found, n is not prime. A more efficient approach is to check whether any of the
* prime numbers less than or equal to 2n can divide n evenly. If not, n is prime.
* Rewrite Listing 5.15 to display the first 50 prime numbers using this approach.
* You need to use an array to store the prime numbers and later use them to check
* whether they are possible divisors for n.
*
* @author Harry Dulaney
*/
public class Exercise07_06 {
public static void main(String[] args) {
final int NUMBER_OF_PRIMES = 50; // Number of primes to display
final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line
int count = 0; // Count the number of prime numbers
int number = 2; // A number to be tested for primeness
System.out.println("The first 50 prime numbers are \n");
// Repeatedly find prime numbers
while (count < NUMBER_OF_PRIMES) {
// Assume the number is prime
boolean isPrime = true; // Is the current number prime?
// Exercise25_01 whether number is prime
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) { // If true, number is not prime
isPrime = false; // Set isPrime to false
break; // Exit the for loop
}
}
// Display the prime number and increase the count
if (isPrime) {
count++; // Increase the count
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
// Display the number and advance to the new line
System.out.println(number);
} else
System.out.print(number + " ");
}
// Check if the next number is prime
number++;
}
}
}