(Usage hints for this presentation)
Winter Term 2020/2021
Dr. Jens Lechtenbörger (License Information)
def naive_mult(op1, op2):
if op2 == 0: return 0
result = op1
while op2 > 1:
result += op1
op2 -= 1
return result
print(naive_mult(2, 3))
def naive_mult(op1, op2)
declares function naive_mult
with two operands==
tests for equality, =
is assignment to variable on leftresult += op1
is short for result = result + op1
op1
is added to result
-=
similarly
def naive_mult(op1, op2):
if op2 == 0: return 0
result = op1
while op2 > 1:
result += op1
op2 -= 1
return result
def naive_exp(op1, op2):
if op2 == 0: return 1
result = op1
while op2 > 1:
result = naive_mult(result, op1)
op2 -= 1
return result
print(naive_exp(2, 3))
naive_mult
is copied from previous slidenaive_exp
shares same basic structure
naive_mult
instead of plus operationnaive_mult
on the
previous slide is reversed, i.e., if naive_mult(op1, result)
instead of naive_mult(result, op1)
is executed?
Except where otherwise noted, the work “Complexity Example”, © 2019-2020 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.
In particular, trademark rights are not licensed under this license. Thus, rights concerning third party logos (e.g., on the title slide) and other (trade-) marks (e.g., “Creative Commons” itself) remain with their respective holders.