<jupyter_start><jupyter_text>Numpy Exercise 2  Imports<jupyter_code>import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Factorial Write a function that computes the factorial of small numbers using `np.arange` and `np.cumprod`.<jupyter_code>def np_fact(n):
    """Compute n! = n*(n-1)*...*1 using Numpy."""
    if n>0:
        array = np.arange(1,n+1) #The arrange function counts by 1 for the range of the array.  With a factorial that range is 1 to n
        x = np.cumprod(array) #This multiplies the values of the array
        y = x[n-1]
    else:
        y = 1 #I couldn't get a function to work with the factorial of 0, so I made it a special case
    return y
#print np_fact(6)        
    #raise NotImplementedError()
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]<jupyter_output><empty_output><jupyter_text>Write a function that computes the factorial of small numbers using a Python loop.<jupyter_code>def loop_fact(n):
    """Compute n! using a Python for loop."""
    if n > 0:  
        n = n+1 #To offset the first step of the while loop
        x = 1
        while n > 1:
            n = n - 1
            x = n*x #Multiply n and subtract 1 from n
    else: #Special case for 0!
        x = 1
    return x
print loop_fact(5)
    #raise NotImplementedError()
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]<jupyter_output><empty_output><jupyter_text>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()```<jupyter_code>%timeit -n1 -r1 np_fact(100000) #I didn't get the expected result until I called larger numbers
%timeit -n1 -r1 loop_fact(100000)
#raise NotImplementedError()<jupyter_output>1 loops, best of 1: 3.44 ms per loop
1 loops, best of 1: 15.2 s per loop
{'fraction_non_alphanumeric': 0.11137553545930509, 'fraction_numerical': 0.07187053783912423, 'mean_word_length': 3.911214953271028, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '252489', 'license': 'MIT', 'n_tokens_mistral': 836, 'n_tokens_neox': 752, 'n_words': 250, 'path': 'assignments/assignment03/NumpyEx02.ipynb', 'repo_name': 'edwardd1/phys202-2015-work', 'size': '2101'}