Question: Here is a naive algorithm for isPrime ( x ) that runs in O ( n ) time, where n is the value of the
Here is a naive algorithm for isPrimex that runs in On time, where n is the value of the input
and k log n is the size of the input
def isPrimen:
INPUT: n is an integer
OUTPUT: True if n is prime, False otherwise
for i in range n:
if ni :
return False
return True
a Suppose we change the algorithm by replacing n in line by n Prove that this algorithm
is incorrect, by showing that there are inputs for which it returns the wrong answer.
NOTE: this is known a proof by counterexample.
b Suppose we change the algorithm by replacing n on line by intsqrtn Prove that this
algorithm is correct, by showing that for all inputs, it returns the correct answer.
c Give an analysis of the running time for algorithm b as a function of input value n
d Using your solution from c find running time for algorithm b as a function of input size k
NOTE: if the input value is n then the input size k is log n Given that log n k taking exponent
of both sides, we get n
k
So you can plug
k
into your function of n to get the function of k
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
