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.
-
Are there possibilities for subscription or paid programming blocks? If so, what are they?
-
What types of changes have financial markets experienced during the last two decades? Have they been perceived as positive or negative changes? Explain. AppendixLO1
-
Sequence the following jobs by (a) SPT (b) DDATE (c) SLACK. Calculate mean flow time, mean tardiness, and maximum tardiness. Which sequencing rule would you recommend?Why? Job Processing Time Due...
-
Current Attempt in Progress Ivanhoe Buttons uses a petty cash imprest system. The fund was established on September 4 with a balance of $190. On September 30, there were $48 cash and the following...
-
A store maintains data on customers, products and purchase records in three tables: CUSTOMER, PRODUCT, PURCHASE. The store manager wants to know which product is on its maximum discount for each...
-
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.
-
For the sake of logic and coherence, Sentence 5 should be placed: F. NO CHANGE G. Lay H. Lies J. Was lying
-
Evaluate your own negotiation way. Do you have one? how you consider having an excellent negotiaiton skill could help any business person to achieve its goals.
-
j. Interest was accrued on the note receivable received on October 17 ($100,000, 90-day, 9% note). Assume 360 days per year. Date Description Dec. 31 Interest Receivable Interest Revenue Debit Credit
-
A Chief Risk Officer (CRO) is interested in understanding how employees can benefit from AI assistants in a way that reduces risk. How do you respond
-
Based on contract law principles, do you think the jury\'s verdict against the Loewen Group for $ 5 0 0 million was appropriate? Why or why not? What factors should the jury have considered in...
-
5.) Consider you have two systems - one filled with (1kg) water and the other with (1kg) of air. Both systems are at 1000 kPa and 30 C. Determine numerically which fluid system has the larger...
-
June lost her case in trial court. She thought that the plaintiff, Sid, had lied during the trial. On appeal, she requested that she be allowed to appear and explain why she thought Sid had lied....
-
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()...
-
PROBLEM 4-23 Basics of CVP Analysis LOL. LOJ.LO4. LO5, 108) Feather Friends Ire distribute a high quality wooden birdhouse that sells for $20 per unit. Vi ale ape 58 par unit and fixed axpenses toul...
-
You purchase a painting today for $8,728. Paintings by this artist are expected to increase in value by 3.65% per year. How much do you expect the painting to be worth in 5 years?
-
Issuing stock Professional Products Inc., a wholesaler of office products, was organized on February 5 of the current year, with an authorization of 75,000 shares of preferred 2% stock, $55 par and...
Study smarter with the SolutionInn App