...

/

Solution Review: Product of Two Positive Integers

Solution Review: Product of Two Positive Integers

This lesson contains the solution review for the challenge to find the product of two numbers.

We'll cover the following...

Let’s discuss the solution to the challenge in the previous lesson. The problem was to find the product of two positive integers.

Implementation

Let’s have a look at the implementation below:

Press + to interact
def recursive_multiply(x, y):
# This cuts down on the total number of
# recursive calls:
if x < y:
return recursive_multiply(y, x)
if y == 0:
return 0
return x + recursive_multiply(x, y-1)
x = 500
y = 2000
print(x * y)
print(recursive_multiply(x, y))

Explanation

The hint indicated the following:

5 * 3 = 5 + 5 + 5 = 15

We make use of the hint in the implementation. Let’s skip the code on ...