Name: Julie Gardner-Hoag, Cynthia Parks
Student ID: 2299636, 2303535
Email: gardnerh@chapman.edu, cparks@chapman.edu
Course: CS510 Fall 2017
Assignment: Classwork 4


```python
import numpy as np    # Numeric python
import sympy as sp    # Symbolic python
import pandas as pd   # Data processing python
import primes


```

# The Sieve of Eratosthenes

The goal of this algorithm is to find all prime numbers up to a number n. This algorithm works by starting at the number 2 and removing all multiple of 2. Then we remove all multiples of 3, and so on.  Once multiples of 3 are removed, we check for the next available number.  This ends up being 5 since 4 was removed during the multiples of 2 process.  This is how we are able to get the primes left behind.

For this function we decided to go with a list because the removal process works well with generating a full list of integers up to n, then picking out all the composite numbers.


```python
def eratosthenes(n):
    primes = []
    if n<=0:
        print("Please enter a positive number")
        return
    count = 2
    while count<=n-1:
        primes.append(count)
        count +=1
    p = 2
    while p<=n:
        for i in range(2, n+1):
            if i*p <= n:
                if i*p not in primes:
                    continue
                else:
                    primes.remove(i*p)
        p +=1
    return primes
```

# Generating Prime Numbers

The function gen_eratosthenes() generates all prime numbers by running the eratosthenes function each implementation, upping the current variable until a new prime is added to the list.  Since this generator uses the original function, the implementation is very similar.


```python
def gen_eratosthenes():
    a = 2
    gen_primes = []
    while True:
        if a is in eratosthenes(a+1):
            gen_primes.append(a)
            yield a
            a += 1
        else:
            a += 1
    return gen_primes
```

# Benchmarking Implementations

Use %time and %timeit magic function to compare functions and explain which one is faster. By how much? Does it match your prediction about efficiency above? Speculate about what is being slow about the slower implementation.
Using the %timeit magic function, we can compare which function is faster.


```python
%timeit eratosthenes(100)
```

    766 µs ± 43.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)



```python
%timeit gen_eratosthenes()
```

This leads us to believe that the generator function works faster because it doesn't need a for loop, thus increasing the efficieny of the code.

9040690b816fe5cb454f858daf0ace1b123e0651
ipynb
Jupyter Notebook
cw04-primes.ipynb
chapman-cs510-2017f/cw-04-julieandcindy
824a9f3581e88d2855f62dcf43b394e5ebe02c80
MIT
cw04-primes.ipynb
chapman-cs510-2017f/cw-04-julieandcindy
824a9f3581e88d2855f62dcf43b394e5ebe02c80
MIT
cw04-primes.ipynb
chapman-cs510-2017f/cw-04-julieandcindy
824a9f3581e88d2855f62dcf43b394e5ebe02c80
MIT
Qwen/Qwen-72B
1. YES
2. YES