Question

In: Computer Science

(a) Assume that a polynomial-time primality testing algorithm, calledPrimes is available, which takes as input a...

(a) Assume that a polynomial-time primality testing algorithm, calledPrimes is available, which takes as input a single numbern >1 and outputs whethernis a primenumber or not. Now consider the following algorithm:

Input: A natural number n > 1

Algorithm Mystery(n)

if ( n mod 2 == 0 ) then

if (n == 2) then

output ‘‘Input is a prime number’’

   else ‘‘Input is not a prime number’’

else

Primes(n)

What is Algorithm Mystery trying to achieve? What is tightest possible lower bound that you can prove for Mystery and why? What is the tightest possible upper bound, assuming Primes(n)runs in quadratic time, that you can prove for Mystery and why? (b) Solve the recurrence T(n) =T(n/2) + 1 with the initial condition T(1) = 1. Show all steps. You may assume that is a power of 2 if it is convenient. Give one example of an algorithm whose time complexity can be expressed by this recurrence. Briefly explain what this algorithm does and how, and also what is its input.

Solutions

Expert Solution

Here is the solution to above problem . Please read code comments for more information

PLEASE GIVE A THUMBS UP!!!

Input: A natural number n > 1


//The Algorithm Mystery checks the initial condition wheather the input number is 2 or not , It basically checks if number is divisible 2
//Hence it is definetely not prime, It is not divisble by 2 the algorithm proceeds to check that if a number is prime or not USING Primes algorithm
//in order N time
Algorithm Mystery(n)
//checking if the number n is divisible by 2
if ( n mod 2 == 0 ) then //
if (n == 2) then //if number is 2 is prime
output "Input is a prime number"
else "Input is not a prime number" //If a number is divisible by 2 and it is not 2 hence it is prime
else
Primes(n) //This check a number is Prime or not O(N^2)

Input: A natural number n > 1


//The Algorithm Mystery checks the initial condition wheather the input number is 2 or not , It basically checks if number is divisible 2
//Hence it is definetely not prime, It is not divisble by 2 the algorithm proceeds to check that if a number is prime or not USING Primes algorithm
//in order N time
Algorithm Mystery(n)
//checking if the number n is divisible by 2
if ( n mod 2 == 0 ) then //
if (n == 2) then //if number is 2 is prime
output "Input is a prime number"
else "Input is not a prime number" //If a number is divisible by 2 and it is not 2 hence it is prime
else
Primes(n) //This check a number is Prime or not O(N)

Input: A natural number n > 1


//The Algorithm Mystery checks the initial condition wheather the input number is 2 or not , It basically checks if number is divisible 2
//Hence it is definetely not prime, It is not divisble by 2 the algorithm proceeds to check that if a number is prime or not USING Primes algorithm
//in order N time
Algorithm Mystery(n)
//checking if the number n is divisible by 2
if ( n mod 2 == 0 ) then //
if (n == 2) then //if number is 2 is prime
output "Input is a prime number"
else "Input is not a prime number" //If a number is divisible by 2 and it is not 2 hence it is prime
else
Primes(n) //This check a number is Prime or not O(N^2)

The tighest bound for mystery algorithm is O(N^2) since all it has are simple if conditions other than that it just calls the primes(n) function which takes O(N^2) time

b) T(N) = T(N/2) + 1

T(N) = T(N/4) + 1+1 //put value of T(N/2)

..

.

.

T(K) = T(N/2k) + K

now solving for N/2k =1 we get K = LOGN

Hence time complexity of T(N) =T(N/2) +1 is O(LOGN)

The Greatest common divisor has the time complexity represented by T(N/2) +1


Related Solutions

Describe a non-deterministic Turing machine algorithm that takes one integer and checks for primality. What is...
Describe a non-deterministic Turing machine algorithm that takes one integer and checks for primality. What is the complexity of the algorithm?
Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer...
Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer is a prime. The input of the algorithm is a large positive integer. The output is “the number *** is a prime” or “the number *** is not a prime”. The error probability of the algorithm should be no more than 1 256 . Use this program to test some big integers. In Java, there is a class BigInteger. You can use methods of...
Show that if P=NP then there is a polynomial-time algorithm for the following search problem: given...
Show that if P=NP then there is a polynomial-time algorithm for the following search problem: given a graph, find its largest clique.
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces...
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces as output a matrix ?={???}M={mij} where ???mij is the minim term in the sequence of integers ??,??+1,…,??ai,ai+1,…,aj for ?≥?j≥i and ???=0mij=0 otherwise. for i := 1 to n for j := 1+1 to n for k:= i+1 to j m[i][j] := min(m[i][j], a[k]) end for end for end for return m a.) Show that this algorithm uses ?(?3)O(n3) comparisons to compute the matrix M....
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Foundation of Computer Science Describe an algorithm that takes as input a list of n integers...
Foundation of Computer Science Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it. Write its corresponding program using your favorite programming language
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size 5. Show that, when the group size is 7, the algorithm still runs in linear time.
takes a single string d as input which represents a date
takes a single string d as input which represents a date
Write a batch program that takes and echoes input data one character at a time until...
Write a batch program that takes and echoes input data one character at a time until EOF is encountered and then prints a summary such as The 14 lines of text processed contained 20 capital letters, 607 lowercase letters, and 32 punctuation marks. Program: C
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT