Question

In: Computer Science

RecursiveFunction(n) // n is an integer { if (n > 0){ PrintOut(n % 2); RecursiveFunction(n /...

RecursiveFunction(n) // n is an integer {

if (n > 0){

PrintOut(n % 2);

RecursiveFunction(n / 3);

PrintOut(n % 3);

}

}

What is the output of this RecursiveFunction Pseudocode algorithm if it is initially called with 10 for n? Explain how you arrived at that answer; by writing call stack for the execution of his function at each step. What is the Big O running time of the RecursiveFunction described above? Explain your answer.

Solutions

Expert Solution



RecursiveFunction(10)
    => print(10 % 2) => print 0
    => RecursiveFunction(3)
        => print(3 % 2) => print 1
        => RecursiveFunction(1)
            => print(1 % 2) => print 1
            => RecursiveFunction(0)
                => Nothing is done here.
            => print(1 % 3) => print 1
        => print(3 % 3) => print 0
    => print(10 % 3) => print 1


Output: 0, 1, 1, 1, 0, 1

**************************************************

I have tried to answer your question to best of my efforts. However, if you still face any issues in the answer, please let me know via the comment section. Your feedback will imporve as well as encourage me to keep up the good work.

If i was able to help you, then please provide me an upvote(thumbs up). Thanks!


Related Solutions

Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
Prove that if n is an integer and n^2 is even the n is even.
Prove that if n is an integer and n^2 is even the n is even.
3.11. (a) Let n be any integer such that n is congruent to 0 (mod 7)....
3.11. (a) Let n be any integer such that n is congruent to 0 (mod 7). For any positive integer k, what is the remainder when n^k is divided by 7? (b) Let n be any integer such that n is congruent to 1 (mod 7). For any positive integer k, what is the remainder when n^k is divided by 7? (c) Let n be any integer such that n is congruent to 2 (mod 7). For any nonnegative integer...
Prove that τ(n) < 2 n for any positive integer n. This is a question in...
Prove that τ(n) < 2 n for any positive integer n. This is a question in Number theory
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
Prove the following theorem. If n is a positive integer such that n ≡ 2 (mod...
Prove the following theorem. If n is a positive integer such that n ≡ 2 (mod 4) or n ≡ 3 (mod 4), then n is not a perfect square.
For all integers n > 2, show that the number of integer partitions of n in...
For all integers n > 2, show that the number of integer partitions of n in which each part is greater than one is given by p(n)-p(n-1), where p(n) is the number of integer partitions of n.
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...
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
Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2
Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT