• edited by
335 views
0 0 votes

Consider the function $\textsf{foo}$ and $\textsf{bar}$ described in pseudocode below, where $/ /$ denotes quotient (integer division) and $\%$ denotes remainder.

int foo (int n) {
    int i = 1;
    while (bar (i) < n){
        i = 2* i;
    }
    return (i);
    }
    
    int bar (int n){
        if (n == 0){
            return (1);
            }
            int x = bar(n // 2);
            if (n % 2 == 0) {
                return (x*x);
            } else {
                return (2*x*x);
            
        }
}
  1. What function does $\textsf{bar (n)}$ compute? Justify your answer.
  2. If $\textsf{foo (n)}$ compute $\textsf{x}$, how do $\textsf{n}$ and $\textsf{x}$ relate to each other. Justify your answer.
  3. How many recursive calls to $\textsf{bar}$ and made in computation of $\textsf{foo(100)}?$

Please log in or register to answer this question.

Position:
Show:

Related questions

0 0 votes
0 0 answers
407
407 views
admin asked Jul 22, 2022
407 views
Let $\text{A}=\left ( \sqrt{3} +\sqrt{2}\right )^{1000}, \text{B}=\left ( \sqrt{3} -\sqrt{2}\right )^{1000}$. Prove the following statements.$\text{A+B}$ is an integer.Th...
0 0 votes
0 0 answers
283
283 views
admin asked Jul 22, 2022
283 views
Imagine you are playing a computer game that consists of different types of coins. You have the power to cast two magic spells $s_{1}$ and $s_{2}$. Each spell consumes so...
0 0 votes
0 0 answers
373
373 views
admin asked Jul 22, 2022
373 views
A cut edge of a connected graph $\text{G}$ is any edge $e=\left \{ u, v \right \}$ of $\text{G}$ such that the graph $\text{G} -e$ obtained by deleting edge $e$ (and not ...
0 0 votes
0 0 answers
285
285 views
admin asked Jul 22, 2022
285 views
For a language $\text{L}$ over an alphabet $\Sigma$, define$$\text{SW(L)}:= \left \{ y \in \Sigma^{\ast} \mid \exists x \in \Sigma^{\ast}\; \text{s.t.}\; xyx \in \text{L}...