Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import sys #import the sys module to access command line arguments #The function below creates a list of all primes less than n def createPrimeNumberArray
import sys #import the sys module to access command line arguments
#The function below creates a list of all primes less than n
def createPrimeNumberArrayn:
primes #create an empty list to store the prime numbers
isPrime True n #create a list of n elements, all set to True
for i in range n: #iterate through the list of numbers from to n
if isPrimei: #check if i is prime
primes.appendi #add the prime number to the list
#starting with i mark all multiples of i as False
#this is a shortcut to help avoid checking redundant multiples
for j in rangei i n i:
isPrimej False
return primes
def totientn primes:
#below calculates the result of the product of p for each prime p that divides n
result
if n in primes:
return n
for prime in primes:
if prime n:
break
if n prime : # determine if prime evenly divides n
result result prime
#At this point, the variable result is the product of p for each prime p that divides n
return result; #THE ERROR IS RIGHT HERE!!! MAKE ONE SMALL CHANGE TO FIX THE ERROR.
#ONCE YOU FIX IT HERE ARE MORE HINTS TO FINISH THE PROGRAM
#The totient function is supposed to return an integer value, not a floating point decimal value.
#You can use the int function to convert the result to an integer before returning it
#make sure the number of command line arguments is correct
numCommandArgs lensysargv
if numCommandArgs : # the program name and the two arguments
printYou must provide an integer n to be evaluated Usage: python sievetotient.py n
sysexit
#make sure the command line argument is an integer greater than
n intsysargv #get the integer from the command line
if n : #make sure the integer is greater than
printThe integer must be greater than
sysexit
#call the functions to create the prime number array and calculate the totient value
primeArray createPrimeNumberArrayn #create the prime number array one was added in case n is a prime number
totientValue totientn primeArray #calculate the totient value
printThere are s prime numbers less than or equal to sformatlenprimeArrayn #print the number of primes less than n
printThe totient function of s is sformatntotientValue #print the totient value of n
# Sieve of Eratosthenes and Euler's Totient Function
This assignment will demonstrate how to implement the Sieve of Eratosthenes and Euler's Totient Function from a very easy sequential Python implementation.
# Product Owner Statement
As a person very interested in mathematics and the Python programming language I decided to create a simple program that will calculate the totient function for a given value. I believe I am close to completing my simple project but my code has an error in it I need your help and knowledge of the General Totient Function Formula to finish my project.
My code runs when I use the command line argument below:
python sievetotient.py
I know my program should give the following:
There are prime numbers less than
The totient function of is
Unfortunately it prints out the following:
There are prime numbers less than
The totient function of is
# Acceptance Criteria
The following must be implemented to receive credit for this assignment.
The code provided does not need to be altered significantly to complete the assignment. Before you start making changes make sure you read the code comments. There may be hints on how to fix the program.
The final output for a totient value should be an integer and correct.
# Have Fun With it
Once you complete the assignment take a moment to recoginize the code is not optimized at all. Yet it is still able to calculate a the totient value for very large values and the required prime list to do it Try calculating the totient function for then one million, and finally ten million. Notice the time differences. Remember Python is very slow. Just imagine implementing this with faster languages using optimizations.
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