import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns

"""
Explanation: Numpy Exercise 2
Imports
End of explanation
"""


def np_fact(n):
    """Compute n! = n*(n-1)*...*1 using Numpy."""
    if n==0:
        c=1                    # this makes it so if 0 is imputed a 1 is reurned
        return c
    else:
        a=np.arange(1,n+1,1)   #aranges a function up to n in 1 increments
        b=np.cumprod(a)        #multiplies each term as it goes up
        c=b[::-1]              #reverses the array of numbers
        d=c[0:1]               #takes the largest of them, the first term
        return d
print np_fact(10)              #ensuring correctness

assert np_fact(0)==1
assert np_fact(1)==1
assert np_fact(10)==3628800
assert [np_fact(i) for i in range(0,11)]==[1,1,2,6,24,120,720,5040,40320,362880,3628800]

"""
Explanation: Factorial
Write a function that computes the factorial of small numbers using np.arange and np.cumprod.
End of explanation
"""


def loop_fact(n):
    K=1                   #defines the variable
    for x in range(1,n+1):#for loop in the range 1-n
        K*=x              #makes the factorial
    return K              
print loop_fact(1)        #ensures it was working

assert loop_fact(0)==1
assert loop_fact(1)==1
assert loop_fact(10)==3628800
assert [loop_fact(i) for i in range(0,11)]==[1,1,2,6,24,120,720,5040,40320,362880,3628800]

"""
Explanation: Write a function that computes the factorial of small numbers using a Python loop.
End of explanation
"""


%timeit -n1 -r1 np_fact(50)    #times the first function
%timeit -n1 -r1 loop_fact(50)  #times the second function

"""
Explanation: Use the %timeit magic to time both versions of this function for an argument of 50. The syntax for %timeit is:
python
%timeit -n1 -r1 function_to_time()
End of explanation
"""


