Question
PYTHON! PYTHON! 1). Multiplication using Addition Write a function: mul(a,b) that will compute and return the product of two positive integers a and b using
PYTHON! PYTHON!
1). Multiplication using Addition
Write a function: mul(a,b) that will compute and return the product of two positive integers a and b using only a for loop and the addition operator ( + ).
Then, write a new function mul2(a,b) that does the same thing, but uses a while loop rather than a for loop.
2). Exponentiation using Multiplication
Write a function expo(x,y) that computes and returns the exponent xy using only a loop and the multiplication operator.
Then, adjust it so that rather than using the multiplication operator, it uses one of the multiplication functions you wrote in part 1.
Hints:
-
xy is equal to (x * x * x * x ... x), y times.
-
When youre computing a sum using a loop, you start at 0, since anything + 0 is itself. When youre computing a product using a loop, you start at ?, since anything * ? is itself (fill in the ?).
-
If you wrote your mul function from part 1 correctly, then the following two lines should have the same effect, so you can use one to replace the other.
-
var = foo * bar
-
var = mul(foo, bar)
-
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started