FAQs on the exercise Multiplication? Schmultiplication!

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!

Agree with a comment or answer? Like () to up-vote the contribution!

def multiplication(num_1, num_2):
result = 0
for count in range(0, num_2):
result += num_1
return result

multiplication(3, 7)

21

multiplication(5, 5)

25

multiplication(0, 4)

0

Is the big O runtime for the above code quadratic because depending on how large your numbers are the for loop will execute more times and then each time the for loop has to execute a linear operation?

Well, as beginners, at least, we generally see computational complexity expressed in terms of a single variable, O(n), O(log(n)), etc.

But your function has two variables! Now, if we hold, for instance, num_1 constant, say 9, and then increase num_2, it would (I think) be linear in num_2, the evaluation of multiplication(9, 1000), taking (more or less) ten times as long as multiplication(9, 100), which in turn takes ten times as long as multiplication(9, 10).

But I don’t think that’s what you are getting at! if you are increasing bothnum_1 and num_2, I would guess that you would see O(num_1 * num_2), although I don’t know how to derive it.

I would like to share my thoughts on the runtime with you guys.
There are many ways to do it I guess and I really liked the idea to get the min and max value.

Here is my code for the recursive function without min and max:

print("\nThis is the recursiv Version:")
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n\n")
def multiplication_rec(n1,n2,result = 0):
if n1 == 0 or n2 == 0:
print("************BASE CASE*******************\n")
return 0
if n1 == 1:
print("************BASE CASE*******************\n")
return n2 + result
if n2 == 1:
print("************BASE CASE*******************\n")
return n1 + result
print("++++++++++++++++Recursive Step++++++++++++++++++++")
print("Adding the secound input number to the result...")
result += n2
return multiplication_rec(n1 -1,n2,result)
n1 = 3
n2 = 7
print("\nThe multiplication of the numbers {0} * {1} = {2}.".format(n1,n2,multiplication_rec(n1,n2)))
print("\n\nThe is multiplication rec function has a linear Runtime of O((N)).")
print("N being the first number.")
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°")
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n\n")

output:

Here is my code with the min and max value:

print("\nThis is the NONE recursiv Version:")
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n\n")
def multiplication(n1,n2):
result = 0
runtime_count = 0
if n1 == 0 or n2 == 0:
print("Runtime count = 1")
return 0
if n1 == 1:
print("Runtime count = 1")
return n2
if n2 == 1:
print("Runtime count = 1")
return n1
samller_number = min(n1,n2)
bigger_number = max(n1,n2)
while samller_number != 0:
runtime_count += 1
result += bigger_number
samller_number -=1
print("Runtime count = {0}".format(runtime_count))
return result
n1 = 7
n2 = 3
print("\nThe multiplication of the numbers {0} * {1} = {2}.".format(n1,n2,multiplication(n1,n2)))
print("\n\nThe is multiplication function has a linear Runtime of O((N = (min(n1,n2)))).")
print("N being the smaller number.")
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°")
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°\n\n")

output:

As you can see the Runtime itself is the same, but N is different.
In the recursive function N = the value of the first number (n1)
In min,max function N = the value of the smaller input number

I guess it helps a lot to define N to evaluate the runtime.

What you have there will be evaluated as a relational expression. Only if the yield is False will it equate to zero. In Python 0 and False are equal. What you want would be,

We cannot lump variables together to make a comparison. They have to be one value compared to another value. In a logical expression, each operand is a comparison, hence the corrected example I gave above.

There are two expressions, not one. The first expression, num_1 or num_2 is the first to be evaluated. If either (or both) of the values is non-zero, the outcome will be True. It will only be False if neither of them is non-zero. The second expression is, if (True === 0) which is False, or if (False === 0) which is True.

Study up on or and and logical operators to get a clearer picture of how they work.

Really? Did you sketch it out on paper, or with logical expressions before you came to that conclusion?

>>> (0 or 0) and True
0
>>> (0 or 1) and True
True
>>> (1 or 0) and True
True
>>> (1 or 1) and True
True
>>>

My mistake. There is no such operator in Python. Common mistake since I work between languages every day and sometimes forget which language I’m talking about. OG stuff.

In Python, and other languages, 0 equates to False, and 1 equates to True.

>>> 1 == True
True
>>> 0 == False
True
>>>

Note how in the logic example that AND short-circuits on falsy, and that OR short-circuits on truthy.

Given that A and B are expressions with discernable truth value.

Then one suggests you do. Find a few sheets of paper and a sharp pencil and create truth tables for AND, for OR, and for NOT. We can help, but it really will mean more if you do this from your own intuition.