Skip to content

Instantly share code, notes, and snippets.

@MohanSha
Created February 13, 2018 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohanSha/424bd512fb021f6d3d60682a5dd11172 to your computer and use it in GitHub Desktop.
Save MohanSha/424bd512fb021f6d3d60682a5dd11172 to your computer and use it in GitHub Desktop.
The Factorial of a positive integer, n, is defined as the product of the sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as being 1. Solve this using both loops and recursion.
# Factorial Finder
# The Factorial of a positive integer, n, is defined as the product of the
# sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as
# being 1. Solve this using both loops and recursion.
def factorial(n):
result = 1
if n == 0:
return result
while n > 0:
result *= n
n -= 1
return result
def factorial_rec(n):
if n == 0:
return 1
else:
return n * factorial_rec(n-1)
def main():
n = 5
result = factorial_rec(n)
print result
result = factorial(n)
print result
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment