Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

factorial recursion python

def fact_rec(n):
	if n < 0:
		return
	elif n <= 1:
		return 1
	else:
		return n*fact_rec(n-1)

answer = fact_rec(4)
if answer == None: 
	print("Cannot calculate factorial of a negative value")
else:
	print(answer)  # 24 = 4x3x2x1 = 4! 
Comment

python recursion factorial

def factorial(n):

    assert type(n) == int, "Invalid input type"
    assert n >= 0, "Input must be non-negative"
    
    if n <= 1:
        return n
    else:
        return n*factorial(n-1)
Comment

recursive factorial python

def recursive_factorial(n: int) -> int:
	if n<=1: return n
    
    return n * factorial(n-1)
Comment

PREVIOUS NEXT
Code Example
Python :: append to csv python 
Python :: display entire row pandas 
Python :: remove duplicates based on two columns in dataframe 
Python :: python reverse string 
Python :: schedule asyncio python 
Python :: You did not provide the "FLASK_APP" environment variable 
Python :: identify null values 
Python :: fastest sort python 
Python :: array search with regex python 
Python :: python pandas cumulative sum of column 
Python :: python get filename without extension 
Python :: pandas change frequency of datetimeindex 
Python :: python sort 2d list 
Python :: matplotlib show percentage y axis 
Python :: convert birth date to age pandas 
Python :: pygame keys pressed 
Python :: plot confidence interval matplotlib 
Python :: nested dict to df 
Python :: numpy correlation 
Python :: finding the format of an image in cv2 
Python :: how to import random module in python 
Python :: plot bounds python 
Python :: python remove a key from a dictionary 
Python :: make coordinate cyclic in python 
Python :: python auto updating clock 
Python :: check django version 
Python :: how to find duplicate numbers in list in python 
Python :: python ssh library 
Python :: python split string regular expression 
Python :: countplot in pandas 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =