[Edu-sig] Prime Numbers...

kirby urner kirby.urner at gmail.com
Fri Feb 17 16:53:17 EST 2017


I have this segment in my Python course (which I wrote, see below) where I
show prime number generator using an iterator written in two ways: as a
generator function and as a class. Just for kicks, I cut and pasted the
code to Codesters and discovered that, while it replicates Python3 syntax,
it's not yet a complete implementation of the language. yield and next are
not supported keyword and builtin respectively. No problem, I put in a
workaround.

http://mybizmo.blogspot.com/2017/02/prime-numbers.html (screen shots)

https://www.codesters.com/preview/4183156df3fa49e1b12a4ab206d280af/
(runnable)

I had my 5th-8th graders run the code having discovered the concept of
"prime number" was already familiar. I threw out some numbers asking if
they were prime. The 8th grade girl was not fooled by 51. I sketched "trial
by division" very briefly on the whiteboard but did not do any careful
reading of the Python code, as at this age it's enough to just eyeball the
stuff and realize it cuts and pastes.

The 2nd example below actually produces 3000 primes, not 30, despite the
comment. :-D

Thanks to Wes for links, still following 'em.

Kirby

PS:  we also use MIT Scratch in this class. I'm still somewhat on a
learning curve with that one. I got it to work for a Martian Math segment:
http://controlroom.blogspot.com/2016/12/more-core.html

What's Martian Math?
http://wikieducator.org/Martian_Math
http://www.4dsolutions.net/satacad/martianmath/toc.html


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 14 14:40:53 2016

@author: Kirby Urner

Create an iterable that gives back successive prime numbers in
two different ways:  as a class and as a generator function.

Trial by division involves accumulating all primes so far and
admitting new candidates to the club only if no prime up to
the sqrt of same, divides with no remainder.
"""

class Primes:

    def __init__(self):
        self.candidate = 1
        self._primes_so_far = [2]  # first prime, only even prime

    def __iter__(self):
        """I'm already an iterator so just return me as is"""
        return self

    def __next__(self):
        """proof I'm an iterator"""
        while True:
            self.candidate += 2    # check odds only from now on
            for prev in self._primes_so_far:
                if prev**2 > self.candidate:
                    self._primes_so_far.append(self.candidate)
                    return self._primes_so_far[-2]
                if not divmod(self.candidate, prev)[1]: # no remainder!
                    break

def primes():
    """generate successive prime numbers (trial by division)"""
    candidate = 1
    _primes_so_far = [2]  # first prime, only even prime
    yield _primes_so_far[-1]
    while True:
        candidate += 2    # check odds only from now on
        for prev in _primes_so_far:
            if prev**2 > candidate:
                yield candidate  # surrender control at this point!
                _primes_so_far.append(candidate)
                break
            if not divmod(candidate, prev)[1]: # no remainder!
                break                          # done looping


#p = Primes()  # class based iterator
#print([next(p) for _ in range(30)])  # next 30 primes please!

p = primes()  # generator function based iterator
print([next(p) for _ in range(3000)])  # next 30 primes please!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20170217/e2adab85/attachment.html>


More information about the Edu-sig mailing list