Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.io . FileWriter; import java.io . IOException; import java.io . PrintWriter; import java.math.BigInteger; import java.util.Random; import java.util.concurrent.TimeUnit; public class Project _ 1 { BigInteger
import java.ioFileWriter;
import java.ioIOException;
import java.ioPrintWriter;
import java.math.BigInteger;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class Project
BigInteger ONE BigInteger.ONE;
BigInteger ZERO BigInteger.ZERO;
BigInteger TWO BigInteger.TWO;
public static void mainString args
new Project;
public Project
BigInteger n;
BigInteger FIRST new BigInteger;
long endTime System.currentTimeMillis TimeUnit.HOURS.toMillis; endTime an hour after current time
long timeDA, timeRA, elapsedTimeDA, elapsedTimeRA;
String outputFile "DAvsRAresults.csv;
PrintWriter output null;
open output stream
try
output new PrintWriternew FileWriteroutputFile;
n FIRST;
while SystemcurrentTimeMillis endTime
Deterministic approach
timeDA System.currentTimeMillis;
boolean primeDA isPrimen;
elapsedTimeDA System.currentTimeMillis timeDA;
String deterministicAnswer primeDA "YES" : NO;
Randomized approach Monte Carlo
timeRA System.currentTimeMillis;
boolean primeRA isProbablyPrimeBigInteger ; fix
catch IOException ex
System.exit;
public boolean isPrimeBigInteger n
BigInteger s nsqrt;
if ncompareToONE return false;
BigInteger i;
for i TWO; icompareTos; i iaddTWO
if nmodicompareToZEROreturn false;
return true;
private long expint b int x
return long Math.powbx;
public boolean isProbablyPrime int n int iterations
if n return false;
if n return true;
Random rand new Random;
for int i ; i iterations; i
int a rand.nextIntn ;
if expa n n
return false;
return true;
public void dataPrinter BigInteger n long timeDA, String deterministicAnswer, long timeRA, String randomizedAnswer
COP Project
Objective
Students will be able to design algorithms using a randomization technique and conduct a comparison
study of the performance of a randomized algorithm vs a deterministic algorithm.
Assignment Problem
Suppose that you are given a positive integer number n and you are asked to conduct a primality test,
ie to determine if n is prime. Design and implement a computational experiment to contrast the
performance of a randomized algorithm vs a deterministic algorithm. The following requirements must
be used in your study.
Please consult the class slides for a discussion on these approaches.
Requirements
Your application will test large numbers stored as BigInteger objects.
The program will run a primality test on consecutive values from FIRST, ie
FIRST, FIRST FIRST FIRST
where FIRST
Two approaches will be run on each value of the sequence above, a deterministic method and a
randomized method.
The deterministic approach will be based on Prog
The randomized method will be based on Corollary of Fermats Little Theorem.
The program will run for one hour, testing, in a loop, values from the sequence above.
In each iteration of the loop, the following steps will be run
o Step Determine if current value is prime using deterministic approach.
o Step Record the time it took to complete Step
o Step Record answer of Step YESNO
o Step Determine if current value is prime using randomized approach.
o Step Record the time it took to complete Step
o Step Record answer of Step YESNO
Values recorded by each approach will be saved in a csv file to be open in Excel later. Format
your csv file as follows:
Value, Time of DA Prime? According to DA Time of RA Prime? According to RA
Value, Time of DA Prime? According to DA Time of RA Prime? According to RA
Value, Time of DA Prime? According to DA Time of RA Prime? According to RA
For example the numbers below are not taken from a real example; they are offered to
illustrate the content of the file
Value, Time of DA Prime? According to DA Time of RA Prime? According to RA
YES, YES
NO NO
NO NO
In Excel, this would look:
Value Time of
DA
Prime?
According to DA
Time of
RA
Prime?
According to RA
YES YES
NO NO
NO NO
i just need help with the code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started