Write a program that obtains the execution time for finding all the prime numbers less than 8,000,000,
Question:
Write a program that obtains the execution time for finding all the prime numbers less than 8,000,000, 10,000,000, 12,000,000, 14,000,000, 16,000,000, and 18,000,000 using the algorithms in Listings 22.5?22.7. Your program should print a table like this:
Listing 22.5PrimeNumbers.java
Listing 22.6EfficientPrimeNumbers.java
Listing 22.7SieveOfEratosthenes.java
Transcribed Image Text:
10000000 8000000 12000000 14000000 16000000 18000000 Listing 22.5 Listing 22.6 Listing 22.7 import java.util.Scanner; 3 public class PrimeNumbers { public static void main(String[] args) { Scanner input = new Scanner(System. in); System.out.print("Find all prime numbers = n, enter n: "); int n - input.nextInt (); 10 11 final int NUMBER_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 12 13 System.out.println("The prime numbers are:"); 14 15 16 // Repeatedly find prime numbers while (number <= n) { // Assume the number is prime boolean isPrime - true; // Is the current number prime? 17 18 19 20 21 // Test if number is prime for (int divisor = 2; divisor <= (int) (Math.sqrt(number)); divisor++) { if (number % divisor =- 0) { // If true, number is not prime isPrime - false; // Set isPrime to false break; // Exit the for loop check prime 22 23 24 25 26 27 28 29 // Print the prime number and increase the count if (isPrime) { count++; // Increase the count increase count 30 31 32 if (count % NUMBER_PER_LINE == 0) { // Print the number and advance to the new line System.out.printf("%7d\n", number); 33 34 35 36 37 else 38 System.out.printf("%7d", number); 39 40 41 42 43 // Check if the next number is prime number++; check next number 44 45 46 47 48 } System.out.println("\n" + count + prime(s) less than or equal to + n);
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 60% (15 reviews)
Program Plan Create a class Exercise2214 which contains method to print prime number calculation tim...View the full answer
Answered By
Keziah Thiga
I am a self motivated financial professional knowledgeable in; preparation of financial reports, reconciling and managing accounts, maintaining cash flows, budgets, among other financial reports. I possess strong analytical skills with high attention to detail and accuracy. I am able to act quickly and effectively when dealing with challenging situations. I have the ability to form positive relationships with colleagues and I believe that team work is great key to performance. I always deliver quality, detailed, original (0% plagirism), well-researched and critically analyzed papers.
4.90+
1504+ Reviews
2898+ 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
-
Write a program that stores names and telephone numbers in a database. Write another program that stores names and addresses in a database. Modify the programs to use external, conceptual, and...
-
Write a program that simulates stable storage. Use two large fixed-length files on your disk to simulate the two disks.
-
Write a program that allows students to schedule appointments at either 1, 2,3,4,5, or 6, o'clock pm. Use an array of six strings to store the names for the time slots. Write a loop that iterates as...
-
Umida Ltd is considering acquiring Trinity Ltd. Both companies are all-equity firms. Umida and Trinity have 5 million and 6 million shares outstanding respectively. Umida generates $2 million in...
-
Is {(1, 1, 1, (0, 1, 1), (0, 0, 1)} a basis for R3? Is {(1, 0, 0), (0, 1, 0), (0, 0, 1)}? A linear space which has a basis with a finite number of elements is said to be finite dimensional....
-
According to Catalyst, 14.3% executive positions in Fortune 500 companies are occupied by women in 2012. A random sample of 180 Fortune 500 executives was selected. a. What is the probability that...
-
In making its judgment in Rewcastle v. Safeway (1989), a case that concerned dismissal of a man who refused to cut his hair to a conventional male length, tribunal made the following remark about the...
-
Exhibit contains the income statements and balance sheets of The Hershey Company for the years ended December 31, 2011, and December 31, 2010. Hershey manufactures and sells products under more than...
-
Holly Sage Company's fixed budget for the upcoming year is based on sales and production of 1 1 , 0 5 0 units. This represents 8 5 % of its productive capacity of 1 3 , 0 0 0 units. The budgeted...
-
Answer the following questions related to the FSMs for the Selective-Repeat protocol with m = 7 bits. Assume the window size is 64. (Figure 23.34): a. The sending machine is in the ready state with S...
-
Write a program that enables the user to add/remove points by clicking the left/right mouse button, and displays a noncrossed polygon that links all the points, as shown in Figure 22.11a. A polygon...
-
Programming Exercise finds a convex hull for a set of points entered from the console. Write a program that enables the user to add/remove points by clicking the left/right mouse button, and displays...
-
Express as the logarithm of a single quantity: 2 log 2 (2x) log2 x 2 . For what values of x is the value of this expression valid? Explain.
-
Factor the expression. 4x+31x+21
-
What was the total cost of Job #1253 for January? * (1 Point) BREAD Co. is a print shop that produces jobs to customer specifications. During January 2019, Job #1253 was worked on and the following...
-
The Greensboro Performing Arts Center (GPAC) has a total capacity of 7,600 seats: 2,000 center seats, 2,500 side seats, and 3,100 balcony seats. The budgeted and actual tickets sold for a Broadway...
-
eBook Current position analysis The bond indenture for the 10-year, 9% debenture bonds issued January 2, 2015, required working capital of $100,000, a current ratio of 1.5, and a quick ratio of 1 the...
-
Explain Below terms 1-Leverage Ratios 2-Profitability Ratios 3-Market Value Ratios 4-Liquidity Ratios 5-Efficiency Ratios
-
Researchers were interested in comparing two methods for estimating tire wear. The first method used the amount of weight lost by a tire. The second method used the amount of wear in the grooves of...
-
Use nodal analysis to determine voltages v1, v2, and v3 in the circuit Fig. 3.76. Figure 3.76 4 S 3i, 2 A 4A
-
In a digital transmission, the sender clock is 0.2 percent faster than the receiver clock. How many extra bits per second does the sender send if the data rate is 1 Mbps?
-
Distinguish between a signal element and a data element.
-
Calculate the value of the signal rate for each case in Figure 4.2 if the data rate is 1 Mbps and c = 1/2. Figure 4.2 a. One data element per one signal element (r = 1) b. One data element per two...
-
Case 9-47 Comprehensive Master Budget; Short-Term Financing; Acquisition of Robotic Equipment (LO 9-2, 9-3, 9-5, 9-6) Skip to question [The following information applies to the questions displayed...
-
Briefly compare and contrast Return on Investment with Residual Income. Be sure to discuss the advantages and disadvantages of each. When might it be more appropriate to use one method over another.
-
How is this done? The answer should be in a T-chart form in 3 different categories: Direct Materials Inventory, Work-in-Process, & Finish Goods Inventory. Rogers Company had inventories at the...
Study smarter with the SolutionInn App