Question

In: Computer Science

A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...

A Mystery Algorithm

Input: An integer n 1

Output: ??

Find P such that 2^p is the largest power of two less than or equal to n.

Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column

and the rightmost entry is the 0th column.

Repeat until P < 0

If 2^pn then

put 1 into column P

set n := n - 2^p

Else

put 0 into column P

End if

Subtract 1 from P

Return the filled-in table

(a) Show a trace of the Mystery algorithm on n = 9

(b) In general, what is the output of the Mystery algorithm? Be clear and concise in your answer.

(c) What is the run time of the Mystery Algorithm? Your answer should be a function of

n. You may assume that “Find P such that 2^p is the largest power of two less than or

equal to n” takes θ(1) time to compute. Show all of your work clearly with complete

sentences; do not leave out any details.

Solutions

Expert Solution

Repeat until P < 0 ==> this statement have a mistake.I think (p>0),so i am assuming p>0

(a) Show a trace of the Mystery algorithm on n = 9

  

Find P such that 2^p is the largest power of two less than or equal to n.:

p=3 because 2^3=8 which is <=9

Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column

and the rightmost entry is the 0th column.

pth column 0th column

(i)3> 0

(ii) 2^3≤9 then

put 1 into column P

1

pth column (3th) 0th column

set n := 9 - 2^3=>1

(iii)Subtract 1 from P => P=2

(i)2>0

(ii)2^2<=1 (false) so else condition

Else

put 0 into column P

1 0

pth column (2th)     0th column

End if

Subtract 1 from P ==>p=1

(i)1>0

(ii)2^1<=1 (false) so else condition

Else

put 0 into column P

1 0 0

pth column (1th) 0th column

End if

Subtract 1 from P ==>p=0

(i)0>0 (condition false)

loop exits and return table as below:

1 0 0

(b) In general, what is the output of the Mystery algorithm?

The myster algorithm finding the largest power of 2 value which is less than or equal to n

if n=9 we get binary representation as (1 0 0 0) ==> value is 8, which highest power of 2 <=n(9)

if n=8 we get binary representation as (1 0 0 0) ==> value is 8, which highest power of 2 <=n(8)

(c)

Given a number n, find the highest power of 2 that is smaller than or equal to n.

Examples :

Input : n = 10
Output : 8

Input : n = 19
Output : 16

Input : n = 32
Output : 32

A simple solution is to start checking from n and keep decrementing until we find a power of 2.

# Python3 program to find highest

# power of 2 smaller than or

# equal to n.

def highestPowerof2(n):

    res = 0;

    for i in range(n, 0, -1):

         

        # If i is a power of 2

        if ((i & (i - 1)) == 0):

         

            res = i;

            break;

         

    return res;

# Driver code

n = 10;

print(highestPowerof2(n));

     

# This code is contributed by mits


Output :

8

Time complexity : O(n). In worst case, the loop runs floor(n/2) times. The worst case happens when n is of the form 2x – 1.


Related Solutions

A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
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.
For each positive integer, n, let P({n}) =(1/2^n) . Consider the events A = {n :...
For each positive integer, n, let P({n}) =(1/2^n) . Consider the events A = {n : 1 ≤ n ≤ 10}, B = {n : 1 ≤ n ≤ 20}, and C = {n : 11 ≤ n ≤ 20}. Find (a) P(A), (b) P(B), (c) P(A ∪ B), (d) P(A ∩ B), (e) P(C), and (f) P(B′). Hint: Use the formula for the sum of a geometric series
Write an algorithm to input a number n, then calculate 13 +2 3 + 33 +...
Write an algorithm to input a number n, then calculate 13 +2 3 + 33 + ... + n3, the sum of the first n cubic numbers, and output the result. 2-Construct a trace table of the algorithm in question 1 with input n=4.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
Prove via induction the following properties of Pascal’s Triangle: •P(n,2)=(n(n-1))/2 • P(n+m+1,n) = P(n+m,n)+P(n+m−1,n−1)+P(n+m−2,n−2)+···+P(m,0) for all...
Prove via induction the following properties of Pascal’s Triangle: •P(n,2)=(n(n-1))/2 • P(n+m+1,n) = P(n+m,n)+P(n+m−1,n−1)+P(n+m−2,n−2)+···+P(m,0) for all m ≥ 0
let p=11. find generator number for p incase of elgmal algorithm. alice selects integer number x=5....
let p=11. find generator number for p incase of elgmal algorithm. alice selects integer number x=5. calculate public and private key for Alice in elgmal algorithm. alice wants to send plaintext "AGE" to bob. assume that alice selects random k values s 6,4,7 respectively for encryption. what is the ciphertext?
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output:...
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output: 2,3,5,7 n=16, output: 2,3,5,7,11,13 In Java please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT