Numpy Exercise 2

Imports


In [3]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns

Factorial

Write a function that computes the factorial of small numbers using np.arange and np.cumprod.


In [6]:
def np_fact(n):
    """Compute n! = n*(n-1)*...*1 using Numpy."""
    # YOUR CODE HERE
    #raise NotImplementedError()
    #creates array with number from 0 to n
    a=np.arange(0,n+1,1.0)
    #sets first spot in array to 1 (so 0!=1)
    a[0]=1
    x = np.cumprod(a)
    #returns the integer value of the nth spot of the cumulative product array
    return int(x[n])

In [7]:
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]

Write a function that computes the factorial of small numbers using a Python loop.


In [4]:
def loop_fact(n):
    """Compute n! using a Python for loop."""
    # YOUR CODE HERE
    #raise NotImplementedError()
    a=1
    fact=[1]
    #mulitplies each value in the range 0 to n by a
    #a redefined as privously mulitplied numbers at end of each iteration
    for i in range(1,n+1):
        a = i*a
        fact.append(a)
    return fact[n]     

loop_fact(10)


Out[4]:
3628800

In [5]:
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]

Use the %timeit magic to time both versions of this function for an argument of 50. The syntax for %timeit is:

%timeit -n1 -r1 function_to_time()

In [10]:
# YOUR CODE HERE
#raise NotImplementedError()
%timeit -n1 -r1 np_fact(50)
%timeit -n1 -r1 loop_fact(50)


1 loops, best of 1: 84.9 µs per loop
1 loops, best of 1: 26.9 µs per loop

In the cell below, summarize your timing tests. Which version is faster? Why do you think that version is faster?

YOUR ANSWER HERE

loop_fact() is faster by about 58 micro seconds. numpy takes more time for these smaller numbers because it has to create each entry in the array then it can do computations where as a loop only has to do the computation with each number as it gets to each number.