Question: Module 0 1 Coding Assignment: The Eratosthenes Method Purpose Prime numbers form an integral part of cryptographic frameworks as they are the basic building blocks
Module Coding Assignment: The Eratosthenes Method
Purpose
Prime numbers form an integral part of cryptographic frameworks as they are the basic building blocks of some of our cryptography algorithms. There are many ways to compute prime numbers. In this homework, you will modify a Python program that computes the prime numbers in a range, then you will analyze its output. Your modifications will include implementing an additional algorithm called the sieve of Eratosthenes method to compute primes. In addition to giving you some practice with prime number generation, this homework will also serve as a gentle introduction to structured programming in Python.
Instructions
Before starting this assignment, do the following:
import math
import matplotlib.pyplot as plt
from timeit import defaulttimer as timer
Provide your implementation of the Sieve of Eratosthenes here
def eratosthenes n:
return None
Function to determine if a number is a prime
Checks all values from to square root of number to find a divisor for number
Can you spot the inefficiency in the loop below?
How might you fix that?
def isPrime num:
stop intmathsqrt num
isPrime True
for j in range stop:
if numj :
return False
return True
BruteForce approach to print all primes in range to n
def bruteforcen:
list
for i in range n:
if isPrimei:
list.appendi
return list
#Code to time the performance of bruteforce
n
start timer
list bruteforcen
end timer
printbruteforce:",end start
printlist
#plot some statistics
plthistlist densityFalse, bins
plttitlePrime Distribution'
pltshowDownload the primes.py Download primes.pyfile.
Rename the file to primes.py without the angle brackets
Open the file using the Virtual Lab or HIGHLY recommended on your computer if you have Python installed.
Once you've completed those steps, take a closer look at the program. The program does the following:
Uses a brute force algorithm to compute all primes from to
Measures the time taken by the algorithm
Plots the frequency of the prime numbers in the given range
As mentioned earlier, you will need to modify this program. Complete steps
Add a method to the code
that will implement the sieve of Eratosthenes method for computing primes. The sieve algorithm as described on Wikipedia is included in the callout below.
Test your method to ensure that it works correctly. Print out or attach the list of primes generated by your program for
Add code that will call your new method along with the brute force method and measure the time taken for both algorithms to generate for
Comment on the results you see. eg Which is greater or smaller? Why does one take more time than the other? How might this scale if we increased n significantly, say to million?
Plot the primes generated by your method in a histogram. You may replace the plot of the brute force algorithm already displayed. Comment on the distribution of primes. eg Are the primes evenly distributed? Do they tend to occur in a specific range plotted?
Submission
Please submit your answers as a zip file. The following items should be included in the zip file:
The modified primes.py file remember to rename it according to the instructions
A text file that has your answerscomments to questions and above.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
