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?
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...
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...
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?
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
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
Can someone explain to me for E1 mechanism, why OH is the one attacking the hydrogen...
Can someone explain to me for E1 mechanism, why OH is the one attacking the hydrogen instead of the conjugate (H2O)? One of the examples that my professor gave me was KOtBu/tBuOH.This one the congugate (tBuOH) is the one attacking the hydrogen instead of the base (KOtBu). I'm confused about if I'm suppose to use the conguate to attack the hydrogen or the base. Please help! He gave me a list of base that will be on the test (NaOH/H2O,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT