Curated · Programming in Python

NAT · 1 mark · Problem PROG-PYTH-0004

Consider the following program written in pseudo-code. Assume that x and y are integers.
Count(x, y) {
    if (y != 1) {
        if (x != 1) {
            print("*");
            Count(x/2, y);
        } else {
            y = y - 1;
            Count(1024, y);
        }
    }
}
The number of times that the print statement is executed by the call Count(1024, 1024) is .

Numerical answer (NAT).

Solve it on GATE DA practice →More Python Programming problems

Show the worked solution\[\boxed{\text{prints}=(\text{values of } y \text{ that run the inner loop})\times(\text{halvings of } x \text{ from 1024 to 1})}\]For a fixed \(y\ne1\), \(x\) goes \(1024\to512\to\cdots\to2\to1\): the print runs 10 times (once for each \(x\ne1\)). Then \(y\) drops by 1 and \(x\) restarts at 1024.\(y\) takes the values \(1024, 1023, \ldots, 2\) before the call with \(y=1\) stops: 1023 values.Total \(=1023\times10=10230\).Answer: 10230

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com