# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

# ## Introduction

# Factorial(n) written as *n!* is the product of all the numbers up to *n*. 
#
# 8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320

# ## Instructions

# Write a function called **factorial** to calculate the factorial of a given number.

# ## Solutions

import numpy as np


# #### No Recursion

def factorial(n):
    '''returns factorial of number n w/out recursion'''
    
    # error checking
    assert n >= 0, 'n must be a nonnegative integer'
    assert type(n) == int, 'n must be a nonnegative integer'
    
    out = 1
    for value in range(1, n+1):
        out *= value
    return out


for i in range(10):
    print('n: {} | result: {}'.format(i, factorial(i)))


# #### Recursion

def recursive_factorial(n):
    '''returns factorial of number n w/recursion'''
    
    # error checking
    assert n >= 0, 'n must be a nonnegative integer'
    assert type(n) == int, 'n must be a nonnegative integer'
    
    # main logic
    if n == 0:   
        return 1
    else:
        output = n * recursive_factorial(n - 1) 
    return output


for i in range(10):
    print('n: {} | result: {}'.format(i, recursive_factorial(i)))

StarcoderdataJupyterScriptsDedupFiltered