In JAVA language: The following images is the PrimeGeneratorDemo class and the P
ID: 3742018 • Letter: I
Question
In JAVA language:
The following images is the PrimeGeneratorDemo class and the PrimeGeneratorAPI interface that has to be implemented:
With the following information, Create a PrimeGenerator Class that implements the above information and also add to the PrimeGeneratorDemo to get OUTPUT at the end of this post.
The final output should look like the SAMPLE RUN above.
import java.util.ArrayList import java.util.Random; import java.util.scanner; * A test bed for the PrimeGenerator implementation k @author Duncan, YOUR NAME * @since 99-99-9999 * @see PrimeGeneratorAPI, PrimeGenerator public class PrimeGeneratorDemo public static void main(String[] args) Scanner cin new Scanner (System.in); System.out.print( "Enter a positive integer ->") int n-cin.nextInt); PrimeGenerator primeSeqnew PrimeGenerator(n,'E'); System.out.printf("Is %d a prime number? %bSn", n, prineseq .isPrime (n)); System . ou t . printf("Prime numbers in [1,8d] are %s.%n" , n, primeSeq ); System.out.printf("The largest prime number in [1.*d] is %d.Sn",n,primeSeq.getMax ()); ArrayList primesprimeSeq.generate(n); System.out.printf("The number of prime numbers in [1,8d] is %d.%n" , n, prime s . size()); //Add code to generate a random prime in [2,n] and the table shown on handout:Explanation / Answer
class PrimeCompare
{
static int cnt,last=0;
double sieveOfEratosthenesT(int n)
{
boolean prime[] = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
double s=System.nanoTime();
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*2; i <= n; i += p)
prime[i] = false;
}
}
for(int i = 2; i <= n; i++)
{
if(prime[i] == true)
last=prime[i];
}
double l=System.nanoTime();
return (l-s);
}
double naive(int n)
{
int p[]=new int[n+1];
double l=System.nanoTime();
for(int i=2;i<=n;i++)
{
flag=0;
for(int j=2;j<i;j++)
{
if(i%j ==0)
{
flag=1;
break;
}
}
if(flag==0)
p[i]=i;
}
double e=System.nanoTime();
return (e-l);
}
int[] sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*2; i <= n; i += p)
prime[i] = false;
}
}
int p[]=new int[n+1];
// Print all prime numbers
cnt=0;
for(int i = 2; i <= n; i++)
{
if(prime[i] == true)
{
last=prime[i];
p[cnt]=prime[i];
cnt++;
}
}
return p;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a positive number ->");
int n=sc.nextInt();
System.out.println("Is"+n+"a prime number ?")
int prime[]=sieveOfEratosthenes(n);
if(last==n)
System.out.print("true");
else
System.out.print("false");
System.out.println("Prime numbers in [1,"+n+"] are");
int k=0;
while(k<cnt && prime[k]!=0)
{
System.out.print(prime[k]+",")
k++;
}
System.out.println("Largest Prime number=" + last);
System.out.println("Random Prime number="+last);
System.out.prinln();
System.out.println("Empirical Analysis between Brute Force vs Sieve of Eratosthenes Prime sequence generation algorithms:")
System.out.println("===========================================================================================")
System.out.println("n Eratosthenes(n) MaxPrime Naive MaxPrime in Naive")
System.out.println("===========================================================================================")
System.out.print("400000 "+sieveOfEratosthenesT(400000)+" "last+" "+naive(400000)+" "+last);
System.out.println("800000 "+sieveOfEratosthenesT(800000)+" "last+" "+naive(800000)+" "+last);
System.out.println("1200000 "+sieveOfEratosthenesT(1200000)+" "last+" "+naive(1200000)+" "+last);
System.out.println("1600000 "+sieveOfEratosthenesT(1600000)+" "last+" "+naive(1600000)+" "+last);
System.out.println("2000000 "+sieveOfEratosthenesT(2000000)+" "last+" "+naive(2000000)+" "+last);
System.out.println("2400000 "+sieveOfEratosthenesT(2400000)+" "last+" "+naive(2400000)+" "+last);
System.out.println("2800000 "+sieveOfEratosthenesT(2800000)+" "last+" "+naive(2800000)+" "+last);
System.out.println("3200000 "+sieveOfEratosthenesT(3200000)+" "last+" "+naive(3200000)+" "+last);
System.out.println("4000000 "+sieveOfEratosthenesT(4000000)+" "last+" "+naive(4000000)+" "+last);
System.out.print("===============================================================================================");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.