-4
Multiplication must be done by successive sums, 6x4= 4+4+4+4+4+4+4+4
Program entry: 6 4 Output from the program: 24
-4
Multiplication must be done by successive sums, 6x4= 4+4+4+4+4+4+4+4
Program entry: 6 4 Output from the program: 24
-1
First you need to create a function with two parameters (factor and multiplying). In the case how the function is recursive, I will name the parameters as x
and times
.
Within this function you must create a condition to check whether the parameter times
is greater than zero. It is this parameter that will tell how many times more the function should be called.
If the parameter is greater than zero, the function should return the value of x
added to the call return of the same function with the same parameters, however, the argument value times
shall have its value decreased.
If the variable value times
is equal to zero, the function must return the value zero. Thus, the final result will be the sum of the returns of all the calls until times
is equal to zero.
def mult(x, times):
return x + mult(x, times - 1) if times else 0
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
Who voted negative could explain what is wrong in the answer?
– JeanExtreme002
I did not negatively, but I understand the reasons for the negatives. The function is correct, but the answer is superficial. To properly answer this question you would have to introduce the AP in Language Arithmetization to then present the Theory of Recursion and only then Practical Recursion. It is a problem best worked in a classroom because it is a very heavy theoretical burden. Don’t be upset, your intention was to teach AP how to create recursive functions but there are some axioms governing the definition of recursive functions.
– Augusto Vasques