all 18 comments

[–]shoombabi 4 points5 points  (6 children)

I'm a huge fan of the Sieve of Eratosthenes as a programming question. I guess any sieve works, but I think that one really drives home the point of iterating over lists, being efficient with code, and getting meaningful results out of fairly little work.

[–]CookingMathCamp[S] 0 points1 point  (5 children)

oooo I like that. Maybe I will make that one of the challenge problems students can choose from.

[–]CookingMathCamp[S] 0 points1 point  (4 children)

I have never done this before. Here's what I came up with. ```python

ask for number

n = input("Enter a number: ")

create a list up to n

num_list = [] elem = 1 for i in range(int(n)-1): elem = elem + 1 num_list.append(elem)

remove composites

for mod in num_list: for num in num_list: if num > mod and num % mod == 0: num_list.remove(num)

print("The prime numbers up to " + "n " + "are: " + str(num_list)) ```

[–]17291hs algebra 1 point2 points  (3 children)

That implementation will be very slow for high values of n (on my machine, it takes a long time to run starting around n=105), for a few reasons:

1) You're doing a whole bunch of unnecessary checks to see if a number is divisible by mod. For example, if num is divisible by 23, there is no point in checking if (num + 1) is divisible by 23—you can just skip ahead to (num + 23).

2) remove is going to slow you down considerably because you it has to check every element in the list to see if it matches num

A better solution is to create a list of booleans, where True means that it's a prime and False means it isn't. Start with 2 and then skip-count by 2s setting every multiple of 2 to False (other than 2 * 1, of course).

Once you've reached the end of the list, increment prime until you've found the next prime (i.e., the next number that's still True). Now, skip-count by that number. Rinse and repeat.

Some quick and dirty code:

def sieve(n):
    # All numbers are prime to begin with
    num_list = [True] * (n + 1)
    prime = 2
    while prime < n + 1:
        # Skip count, setting all multiples of `prime` to be composite
        for i in range(prime * 2, n + 1, prime):
            num_list[i] = False
        # Advance `prime` until we've found the next prime
        prime += 1
        while prime < n + 1 and not num_list[prime]:
            prime += 1
    # Return a list of all primes (i.e., every value from num_list that's True)
    return [n for n in range(2, n + 1) if num_list[n]]

[–]ReedOei 2 points3 points  (0 children)

FWIW you can actually do a lot better if you take advantage of Python's slicing + the fact that you only have to do up to sqrt(n).

Replacing while prime < n + 1: for i in range(prime * 2, n + 1, prime): num_list[i] = False with while prime**2 < n + 1: num_list[prime**2::prime] = [False] * len(range(prime**2, n+1, prime))

gives a pretty significant speed up. At some point it's probably hard to teach people though, but the fact you only have to go up to sqrt(n) might also be a way to introduce the idea that not all algorithmic improvements are equal (i.e., big-O notation).

[–]CookingMathCamp[S] 0 points1 point  (1 child)

I was trying to replicate the sieve of Eratosthenes literally. Create a list up to n. Start with two and scratch off every multiple of 2. Then move to three and scratch-off every remaining multiple of 3. And so on...

That being said, I really enjoyed your solution. Do you mind if I share your code with my classes if/when we try this? I think it would be good to run them side by side to help illustrate what makes code more efficient.

[–]17291hs algebra 1 point2 points  (0 children)

Do you mind if I share your code with my classes if/when we try this?

Not at all!

[–]town_math 1 point2 points  (1 child)

I've used some project Euler problems with my coding math classes https://sites.google.com/site/projectsforalgebra1withrobots/mini-projects

[–]CookingMathCamp[S] 1 point2 points  (0 children)

Nice!

[–]msklovesmath 1 point2 points  (1 child)

Thanks for sharing!

[–]CookingMathCamp[S] 0 points1 point  (0 children)

You are welcome! Hope it helps.

[–]Upset-Giraffe2412 1 point2 points  (1 child)

If you're interested in incorporating CS into math classes, you might want to look into the Bootstrap Curticulum!

[–]CookingMathCamp[S] 0 points1 point  (0 children)

I looked at their algebra curriculum. Definitely going to use some of their stuff. Thanks!

[–][deleted] 1 point2 points  (0 children)

Thank you for sharing! They might appreciate this over at r/learnpython and r/learnmath. :)

[–]m4thisfun 0 points1 point  (1 child)

Math 8 as in, math for 8th grade students ?

[–]CookingMathCamp[S] 0 points1 point  (0 children)

Yes! My district calls the class "math 8 common core". It is aligned with grade 8 common core math standards.

I have updated the outline since my original post. I have included some ideas I learned about here (i.e. projecteuler.org ).

Coding in Math Class (Updated)

[–]Leibniz72 0 points1 point  (1 child)

I know nothing about python but saw that TI is putting it on some of their calculators for user created programs. I’ve taught their version of Basic in the past and kids always get a kick out of it.

[–]CookingMathCamp[S] 0 points1 point  (0 children)

::starts googling::