Listing 5.15 determines whether a number n is prime by checking whether 2, 3, 4, 5, 6,
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 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
Transcribed Image Text:
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 \n"); // 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 <= number / 2; divisor++) { if (number % divisor == 0) { // If true, number is not prime isPrime = false; // Set isPrime to false break; // Exit the for loop 22 // Display the prime number and increase the count if (isPrime) { count++; // Increase the count 23 24 25 26 27 if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { // Display the number and advance to the new line System.out.println(number); 28 29 30 else System.out.print (number + " "); 31 32 33 34 35 36 37 38 39 } // Check if the next number is prime number++; The first 50 prime numbers are 2 3 57 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 60% (5 reviews)
This java program uses to store the prime numbers and later use them to check whether t...View the full answer
Answered By
Charles mwangi
I am a postgraduate in chemistry (Industrial chemistry with management),with writing experience for more than 3 years.I have specialized in content development,questions,term papers and assignments.Majoring in chemistry,information science,management,human resource management,accounting,business law,marketing,psychology,excl expert ,education and engineering.I have tutored in other different platforms where my DNA includes three key aspects i.e,quality papers,timely and free from any academic malpractices.I frequently engage clients in each and every step to ensure quality service delivery.This is to ensure sustainability of the tutoring aspects as well as the credibility of the platform.
4.30+
2+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Revise Listing 3.8, Lottery.java, to generate a lottery of a threedigit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the...
-
Write a program that reads a graph from a file and determines whether the graph is connected. The first line in the file contains a number that indicates the number of vertices (n). The vertices are...
-
Modify Listing 28.1, TestGraph.java, to create a file representing graph1. The file format is described in Programming Exercise 28.1. Create the file from the array defined in lines 8?21 in Listing...
-
With respect to strategies used by land conservation groups to preserve land, conservation easements seem to be expanding more rapidly than buying land for preservation. In what respect might...
-
Let A = {0, 2, 4, 6, 8, 10}, B = {0, 1, 2, 3, 4, 5, 6}, and C = {4, 5, 6, 7, 8, 9, 10}. Find a) A B C. b) A B C. c) (A B) C. d) (A B) C.
-
Table 3.3 provides information about 200 school leavers and their destination after leaving school. Determine the probabilities that a person, selected at random: (a) Went into full-time education...
-
Five projects form the mutually exclusive, collectively exhaustive set under consideration. The cash flow profiles for the five projects are given in the table below. Information on each project was...
-
Following are preacquisition financial balances for Padre Company and Sol Company as of December 31. Also included are fair values for Sol Company accounts. On December 31, Padre acquires Sols...
-
Max Labs Inc. current portfolio of products has a 10% IRR and a 10% standard deviation. The correlation coefficients with the firm portfolio are 0.5 (r=0.5) for both, Dog Treats and Dog Food; zero...
-
For this assignment, students need to identify the ASC (i.e., four numbers) that can serve as authoritative guidance for the issue raised in each of the cases below and provide a short response. 1....
-
Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is...
-
Write a program that generates 100 random integers between 0 and 9 and displays the count for each number.
-
Define P/O ratio and indicate why it is important.
-
Ying Import has several bond issues outstanding, each making semiannual interest payments. The bonds are listed in the following table. Bond Coupon Rate Price Quote Maturity Face Value 1 5.8 % 105.56...
-
2. What is the future value of an annuity retirement account if you deposit $2500 every year for 35 years? Assume the interest rate is 5.5% per year.
-
In 2013, Senate Democrats then in the majority triggered what was called the "nuclear option" for the first time. Frustrated with what they considered the relentless Republican obstruction of...
-
Establish the factors that must be taken into consideration and take into account the factors of sustainability, differentiation and growth?
-
A mass of 200 gms hangs from the rim of a wheel of radius 5 cm. Mass falls from rest to 2m in 5 sec. find the moment of inertia of the wheel?
-
Why is audit quality an important issue in corporate governance?
-
TRUE-FALSE QUESTIONS 1. In terms of preliminary analytical procedures, assume that the company has introduced a new product with a low price point and significant customer demand. The auditor would...
-
The Huntington High School basketball team has five players named Art, Bob, Cal, Dan, and Eli. Accept the number of points scored by each player in a game and create a bar chart that illustrates the...
-
Modify the BarChart program to accept the number of points scored by each player in a season. The bar chart displays one asterisk for each 10 points scored by a player. For example, if a player has...
-
a. Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set()...
-
Assume that the real rate of interest is 1 . 7 3 percent and the anticipated percentage change in price levels over the life of the loan contract is 9 . 2 3 percent. What is the nominal interest rate...
-
Samsung has been producing the new Galaxy X250 smart phone throughout 2022 but doesn't plan to sell it until March 2023. How are these smartphones, which are sitting in a warehouse somewhere in...
-
Silver Water Motorworks produces small electric motors in lots of 200 with an average yield of 92%. Fifty per cent (50%) of the sub-standard output from the first run through, can be reworked. Direct...
Study smarter with the SolutionInn App