Question

In: Computer Science

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 P

End if

Subtract 1 from P

Return the filled-in table

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

(b) 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.

Solutions

Expert Solution

a) CASE STUDY:
Let us Consider n=5.
Then, 4 is the closest power of 2 to 5.
Therefore, P=2 as 2^2=4.

We create a one-Dimensional array of 3 columns, arranged from MSB(P) to LSB(0). P+1, here represents the minimum no. of bits required to represent the Number n.
Here we create an array of size 3, where the first index is 2 and the last index is 0.

Dry Run

n=5,P=2
We Put 1 into the current column no. if 2P<= n and then decrease n by 2^P
n=5, A[2]= 1
n=5-2*2=1, Decrease P by 1 every time
P=2-1=1
Continue until P <0


n=1, P=1
If (2*1<=1) false
A[1]=0
P=P-1=0
Continue until P <0

n=1, P=0
If(2*0<=1) true
A[0]=1
n=1-2*0=1
P=0-1=-1
Continue until P <0

Exit.

The program returns: 101 marked from 2 to in an Array
Therefore, the general work of the program is to return the binary value of the given number n.

b) The algorithm basically runs it's main loop for the number of times of the nearest power of 2 of the given number n, which is basically = floor((log2n))+1 or generically speaking in terms of n Θ(log n).


Related Solutions

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^p≤n then put 1 into column P set n := n - 2^p Else put 0 into column P End if Subtract 1...
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 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.
[C] 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:
6.19 LAB: Convert to binary - functionsWrite 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. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
(Python) 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 Python8.21 Program: Convert to binary. Sections 2.7, 3.8, 5.2. Functions.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. You will need to write a second function to reverse the string.Your program must define and call the following two functions. The function integer_to_reverse_binary() should return...
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