Question: Write in java please! 5 . 4 0 The Sieve of Eratosthenes is a method used to compute all primes less than N . Begin

Write in java please!
5.40 The Sieve of Eratosthenes is a method used to compute all primes
less than N. Begin by making a table of integers 2 to N. Find the
smallest integer, i, that is not crossed out. Then print i and cross out i,
2i,3i,dots When i>N2, the algorithm terminates. The running time
has been shown to be O(NloglogN). Write a program to implement
the Sieve and verify the running time claim. How difficult is it to dif-
ferentiate the running time from O(N) and O(NlogN)?
Calculating the actual run time for a given size N
For 5.40 and 5.43 you are asked to verify the run time of algorithms. To do this you need to run your code for varying sizes of N and
plot them. There is a library function which is part of java.lang.System called timeNano() System (Java Platform SE 8)_
(
oracle.com) which returns the current number of nanoseconds expressed a long. Before running your code, store this value. After
running the code, find the difference between the current time and the start time to get the elapsed time. For example
long startTime=System.nanoTime ();
sieveofEratosthenes (1000);
// Express elapsed time in seconds
double elapsedTime=(double)(System. nanoTime ()-startTime)/1E+09;
System.out.printf("Elapsed Time is 8.3f
", elapsedTime);
This gives you one data point for a chart like 5.1 and 5.2. You will need to run several times with varying sizes of N to get enough
points to plot the curve. The step you choose for varying N will depend upon the problem and 5.40 will take quite a few to
distinguish between O(N) straight line and O(NlogN) which curves slightly away from O(N).
Put your entries into an Excel or other spreadsheet to graph the results and turn that in for each problem.
 Write in java please! 5.40 The Sieve of Eratosthenes is a

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!