Multiplication Made Simple

Chris wrote a multiplication function which he believes is the next big thing.

1
2
3
4
def mult(a,b):
    if b == 0:
        return 0
    return a + mult(a,b-1)

Which of the options best describes​ the domain of a a and b b respectively, such that the above function works as intended?

Real numbers; Integers Real numbers; Real numbers Real numbers; Non-negative integers Non-negative integers; Integers Integers; Integers Positive integers; Non-negative integers Real numbers; Positive integers Integers; Non-negative integers

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Jaydee Lucero
Jul 13, 2016

The given function is a recursive function, with b b decremented by 1 1 at each loop. For the function to work properly, b b must end with 0 0 which is a nonnegative integer, which implies that b b must be a nonnegative integer . Otherwise, the function will never reach b = 0 b=0 and hence will create an infinite loop.

On the other hand, a a is not affected by any operation contained in the function. Hence, we can say that a a must be a real number .

展豪 張
Jul 12, 2016

Intuitively this programme is to break a × b a\times b into a + a + + a a+a+\cdots+a . Clearly this works only for positive b b and 0 0 (case 0 0 is handled by the if statement).
The answer is: a a is real number, b b is non-negative integer.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...