Question

In: Computer Science

Can someone please explain to me why "return 1" in this Python code returns the factorial...

Can someone please explain to me why "return 1" in this Python code returns the factorial of a given number? I understand recursion, but I do not understand why factorial(5) returns 120 when it clearly says to return 1.

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

Solutions

Expert Solution

ANS:

#factorial function
def factorial(n):
    #if n equals 0
    if n == 0:
        #returns 1
        return 1
    #if n not equals 0
    else:
        #returns n * factorial(n-1)
        return n * factorial(n - 1)


      
return 1 is applicable only if n equals 0
otherwise the else block will be executed

Example:
   factorial(5) is called
  
   n = 5 and not equals 0
   else block is executed
   ie., factorial(5) returns 5 * factorial(5 - 1) = 5 * factorial(4)
  
   now factorial(5) = 5 * factorial(4)
  
   in the else block factorial(4) is called
   n = 4 and not equals 0
   else block is executed
   ie., factorial(4) returns 4 * factorial(4 -1) = 4 * factorial(3)
  
  
   now factorial(5) = 5 * 4 * factorial(3)
  
   factorial(3) = 3 * factorial(3 - 1) = 3 * factorial(2)
  
   now factorial(5) = 5 * 4 * 3 * factorial(2)
  
   factorial(2) = 2 * factorial(2 - 1) = 2 * factorial(1)
  
   now factorial(5) = 5 * 4 * 3 * 2 * factorial(1)
  
   factorial(1) = 1 * factorial(1 - 1) = 1 * factorial(0)
  
   now factorial(5) = 5 * 4 * 3 * 2 * 1 * factorial(0)
  
   factorial(0) is called
   n == 0
   if block is executed
   ie., factorial(0) returns 1
  
   now factorial(5) = 5 * 4 * 3 * 2 * 1 * 1
  
   no more function calls.
   ie., factorial(5) returns 5 * 4 * 3 * 2 * 1 * 1
   ie., factorial(5) returns 120
       


Related Solutions

Can someone please explain to me why a decrease in bad debt expense is a concern...
Can someone please explain to me why a decrease in bad debt expense is a concern or if an increase in debt expense is a concern as well. For example if in the previous year the company had 162,344 but now the current year it has 148,252. What is the inherent risk here with this?
Could someone please tell me what corrections I should make to this code. (Python) Here are...
Could someone please tell me what corrections I should make to this code. (Python) Here are the instructions. Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating I can't get the find function to work and I have no idea how to even go about it. For example, when type in 'find' and...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When the ball moves the paddles don't move and when the paddles move the ball doesn't move. from graphics import * import time, random def racket1_up(racket1):    racket1.move(0,20)        def racket1_down(racket1):    racket1.move(0,-20)   def racket2_up(racket2):    racket2.move(0,20)   def racket2_down(racket2):    racket2.move(0,-20)   def bounceInBox(shape, dx, dy, xLow, xHigh, yLow, yHigh):     delay = .005     for i in range(600):         shape.move(dx, dy)         center = shape.getCenter()         x = center.getX()         y = center.getY()         if x...
Can someone please explain the edgeworth box to me by typing in a way i can...
Can someone please explain the edgeworth box to me by typing in a way i can see and understand? what determines the contract curves path?
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
Hello can someone please explain the is lm curves step by step for me?
Hello can someone please explain the is lm curves step by step for me?
Can someone please explain to me the steps in journalizing events in accounting and then turning...
Can someone please explain to me the steps in journalizing events in accounting and then turning them into income statements. Like which assets and liabilities are credits and which are debits?
why me is me ,why not someone else
why me is me ,why not someone else
Can someone please walk through this program for me and explain out the outputs are generated....
Can someone please walk through this program for me and explain out the outputs are generated. #include <iostream> #include <iomanip> using namespace std; const int size = 5; int fun(int arz[size], int jump) { int i, j = 0; for(i = 0; i < size; i += jump) j += arz[i]; return j; } main() { int arx[size] = {1, 2, 4, 8, 16}; int ary[size] = {10, 20, 40, 80, 160}; cout << fun(arx, 1) << endl; cout <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT