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....
-
What was the final outcome in the Colin Gourley case? Who is Oliver Diaz and why is he in this documentary? ( note there are 2 parts to this question ) What was the final outcome in Jamie Leigh...
-
MicroStrategy, Inc., is a software company that sells its services to other companies under multiyear contracts (on average for three years). The contracts call for a minimum fee, paid monthly, and...
-
Consider the ANOVA table that follows. a. Determine the standard error of estimate. About 95% of the residuals will be between what two values? b. Determine the coefficient of multiple determination....
-
Two regular six-sided dice are tossed. Compute the probability that the sum of the pips on the upward faces of the two dice is the following. (See the figure below for the sample space of this...
-
Allie has bought a new apple orchard. The orchard has a single file of trees, numbered from 1 to N. Each tree has a certail number of ripe apples. Allie has a rule she wants to follow. She wants to...
-
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...
-
What are the defining characteristics of the Bretton Woods international monetary system?
-
Define a piece of text in HTML that will be used for our mouseover purposes. When the user does amouse over or a mouse out the text be will change in the following ways: The font size will either...
-
ou ( the learner ) to manage budget and financial plans Ms . Jones runs a private school. She intends to open a new branch in Springvale. The school is going to constitute 5 0 classrooms and couple...
-
Six-sevenths of Jesse Black's inventory was destroyed by fire. He sold the remaining part, which was slightly damaged, for nine-elevenths of its value and received $2400. (a) What was the value of...
-
Find out the degree of freedom of the following mechanisms. d
-
Suppose a, b, and c are int variables and a = 5 and b = 6. What value is assigned to each variable after each statement executes? If a variable is undefined at a particular statement, report UND...
-
Based on the following information, calculate the expected return and standard deviation for each of the following stocks. What are the co-variance and correlation between the returns of the two...
-
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...
-
Operating income after taxes 28,000 Net financial expense after taxes 2,600 Unrealized gain (loss) from securities , net 450 Gain (loss ) from currency translation , net (400) What is comprehensive...
-
You now move on to your next task - how to account for the cost of repairs or replacements on the footwear. As part of Jordan's public customer charter, it either offers a warranty that repairs the...
-
The Buying Power Index is a very powerful tool for Business decisions; especially business expansions decisions.O a . TRUE O b . FALSE
Study smarter with the SolutionInn App