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
2897+ 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....
-
The current and voltage characteristic of a tungsten filament light bulb are measured and recorded in the following table. Voltages are DC steady-state values, applied for a long enough time for the...
-
Consider the following cash flow profile and assume MARR is 10 percent/year. a. What does Descartes' rule of signs tell us about the IRR(s) of this project? b. What does Norstrom's criterion tell us...
-
Krauss Leasing Company signs a lease agreement on January 1, 2011, to lease electronic equipment to Stewart Company. The term of the non-cancelable lease is 2 years, and payments are required at the...
-
While many parties were interested in acquiring MCI, the major players included Verizon and Qwest. U.S.-based Qwest is an integrated communications company that provides data, multimedia, and...
-
Based on a careful work study in the Hofstetter crop, the result shown in the following table have been observed: a) Compute the normal time for each work element. b) If the allowance for this type...
-
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...
-
a. What is an FHA-insured loan? b. What is a conventional loan?
-
The following information is provided to you: a) current market price of the share= $24 b) Recent Dividend per share = $1 c) Risk-free rate = 4 percent; d) Estimated beta = 1.2 e) Equity risk premium...
-
Re-write each of the following as an equivalent expression that uses addition and then simplify. a. 10-14.5 b. -10-14.5 c. -10-(-14.5) d. 10-(-14.5)
-
a profit maximizing firm has a technology with the production function f(x1,x2) =x1^0.5 x2^0.5 can only use 4 units of x2 in the short run. what is the optimal amount of x1 to use in the short run if...
-
Heidi C. sells golf putters specifically designed for petite women. The company's accountant prepared the following income statement for the company's most recent quarter. Sales Revenue $525,000 Cost...
-
Bon Vivant has just issued bonds whose face value on the market is $200,000. The bonds mature in 10 years and carry an interest rate of 5%. Interest is payable semi annually. The market rate is 4%....
-
Paul Reimer does not keep a personal record of his bank account and does not see the need to do a bank reconciliation. He says he can always use on-line banking to look up the balance in his bank...
-
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...
-
3. Prove that Sin2x = 2corr CSC x is an identity. 4. Determine the solutions to the equation tan x = 3 - 2tanx for 0 x 2 accurate to two decimal places. 5. A sine function has an amplitude of 3, a...
-
Accountability refers to what you need to feel accountable/ responsible/ answerable for pursuing your goal. Instructions: In the space provided, below, indicate how you will be accountable for...
-
What is the gross yearly income? 2. What is the gross monthly income using this pay rate?
Study smarter with the SolutionInn App