ungemmed: (Default)
ungemmed ([personal profile] ungemmed) wrote in [community profile] intro_to_cs 2009-11-12 05:48 am (UTC)

Here are mine! They're a lot like [personal profile] elz's, but I'm only checking factors up to the square root. (Since all factors above the square root are "balanced" by factors below it, if I haven't encountered any factors by the time I get to the square root I'm not going to encounter any.)

Problem 1

import math

primes = [2]
x = 3
while len(primes) < 1000:
    divisible = False
    root = math.sqrt(x)
    for y in primes:
        if x % y == 0:
            divisible = True
            break
        if y > root:
            break
    if not divisible:
        print x
        primes.append(x)
    x += 2

Post a comment in response:

This community only allows commenting by members. You may comment here if you're a member of intro_to_cs.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting