Using Recursion (Python loop)

Level 2

This is the question: Fill in the recursive step in this function, and then find the answer by evaluating

Hint: Remember the definition of factorials and use it to relate and In the code, you will relate factorial(n) and factorial(n-1).

This is the orginal code:

  def factorial(n):
    # Solve the base case
    if n == 0 or n == 1:
       return 1

else:
    return # How can you reduce this problem?

print(factorial(10))

I read the solution but is not said how the code should look like. I'll appreciate help :)


The answer is 3628800.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Jonathan Eshetu
Nov 8, 2020

I believe the else line should say else: return n * factorial(n-1) Also, aren't the else and return supposed to be indented?

good solution

num IC - 7 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...