Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA - Write a program that obtains the execution time for finding all the prime

ID: 3826130 • Letter: J

Question

JAVA - 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. Enter the max value from the keyboard. Start your timer after the input is read from the keyboard and end it after the prime numbers print out. Compare the results with and without output.

Fill out the chart for each algorithm, both with output and without. Once you have filled out the table (see pg. 857), analyze the results. Explain the trends and differences that you notice in the runtimes and why those things are happening.

I'm assuming listing referres to the noted sections on the page.





PaoRanning innoses BST

Explanation / Answer

Code:-

import java.util.*;
import java.lang.*;
import java.io.*;

class Prime
{
void prime_numbers(long n)
{
long temp=n + 1;
boolean p_num[] = new boolean[temp];
for(long i=0;i<n;i++)
p_num[i] = true;

for(long p = 2; p*p <=n; p++)
{
if(p_num[p] == true)
{
  
for(long i = p*2; i <= n; i += p)
p_num[i] = false;
}
}

// Print all prime numbers
for(long i = 2; i <= n; i++)
{
if(p_num[i] == true)
System.out.print(i + " ");
}
}

public static void main(String args[])
{   
Prime obj=new Prime();
long n = 30;
System.out.print("The list of prime numbers ");
System.out.println("less than " + n);
obj.prime_numbers(n);
}
}