Question: 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
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 ?n 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.
Listing
![1 public class PrimeNumber { public static void main(String[] args) { final](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/11/636a726aada52_490636a726a9db39.jpg)

1 public class PrimeNumber { 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 2 3 4. 0; // Count the number of prime numbers 5 int count int number = 2; // A number to be tested for primeness System.out.println("The first 50 prime numbers are "); // Repeatedly find prime numbers while (count < NUMBER_OF_PRIMES) { // Assume the number is prime boolean isPrime = true; // Is the current number prime? 10 11 12 13 14 15 16 17 18 19 20 21 // Test whether number is prime for (int divisor = 2; divisor
Step by Step Solution
3.47 Rating (163 Votes )
There are 3 Steps involved in it
This java program uses to store the prime numbers and later use them to check whether t... View full answer
Get step-by-step solutions from verified subject matter experts
