Question: Java If you could please comment within the StopwatchClient program what your thoughts are and what is occurring thank you. Create a StopwatchClient that measures

Java

If you could please comment within the StopwatchClient program what your thoughts are and what is occurring thank you.

Create a StopwatchClient that measures the running 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 PrimeNumbers.java Download PrimeNumbers.java and EfficientPrimeNumbers.java. You will also need the Stopwatch,java data type.

Your program should print a table like this:

Program Print Output

8000000

10000000

12000000

14000000

16000000

18000000

Prime Numbers
Efficient Prime Numbers

public class Stopwatch {

private final long start;

/**

* Initializes a new stopwatch.

*/

public Stopwatch() {

start = System.currentTimeMillis();

}

/**

* Returns the elapsed CPU time (in seconds) since the stopwatch was created.

*

* @return elapsed CPU time (in seconds) since the stopwatch was created

*/

public double elapsedTime() {

long now = System.currentTimeMillis();

return (now - start) / 1000.0;

}

/**

* Unit tests the {@code Stopwatch} data type.

* Takes a command-line argument {@code n} and computes the

* sum of the square roots of the first {@code n} positive integers,

* first using {@code Math.sqrt()}, then using {@code Math.pow()}.

* It prints to standard output the sum and the amount of time to

* compute the sum. Note that the discrete sum can be approximated by

* an integral - the sum should be approximately 2/3 * (n^(3/2) - 1).

*

* @param args the command-line arguments

*/

public static void main(String[] args) {

int n = Integer.parseInt(args[0]);

// sum of square roots of integers from 1 to n using Math.sqrt(x).

Stopwatch timer1 = new Stopwatch();

double sum1 = 0.0;

for (int i = 1; i <= n; i++) {

sum1 += Math.sqrt(i);

}

double time1 = timer1.elapsedTime();

StdOut.printf("%e (%.2f seconds) ", sum1, time1);

// sum of square roots of integers from 1 to n using Math.pow(x, 0.5).

Stopwatch timer2 = new Stopwatch();

double sum2 = 0.0;

for (int i = 1; i <= n; i++) {

sum2 += Math.pow(i, 0.5);

}

double time2 = timer2.elapsedTime();

StdOut.printf("%e (%.2f seconds) ", sum2, time2);

}

}

-------------------------------------------------------------------------------

public class PrimeNumbers {

public static void main(String[] args) {

int n = 8000000;

int count = 0; // Count the number of prime numbers

int number = 2; // A number to be tested for primeness

// Repeatedly find prime numbers

while (number <= n) {

// Assume the number is prime

boolean isPrime = true; // Is the current number prime?

// 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

}

}

// Print the prime number and increase the count

if (isPrime) {

count++; // Increase the count

}

// Check if the next number is prime

number++;

}

}

}

--------------------------------------------------------------------

public class EfficientPrimeNumbers {

public static void main(String[] args) {

int n = 8000000;

// A list to hold prime numbers

java.util.List list

= new java.util.ArrayList<>();

int count = 0; // Count the number of prime numbers

int number = 2; // A number to be tested for primeness

int squareRoot = 1; // Check whether number <= squareRoot

// Repeatedly find prime numbers

while (number <= n) {

// Assume the number is prime

boolean isPrime = true; // Is the current number prime?

if (squareRoot * squareRoot < number) {

squareRoot++;

}

// Test whether number is prime

for (int k = 0; k < list.size()

&& list.get(k) <= squareRoot; k++) {

if (number % list.get(k) == 0) { // If true, not prime

isPrime = false; // Set isPrime to false

break; // Exit the for loop

}

}

// Print the prime number and increase the count

if (isPrime) {

count++; // Increase the count

list.add(number); // Add a new prime to the list

}

// Check whether the next number is prime

number++;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!