[Tutor] Finding prime numbers

Shawn Milochik Shawn at Milochik.com
Wed Sep 19 13:13:24 EDT 2007


Here's my attempt:

#!/usr/bin/env python

import math

for x in range(3,1000,2):

    isPrime = True

    for y in range(3,(math.sqrt(x) + 1)):
        if x % y == 0:
            isPrime = False
            break

    if isPrime:
        print "%d is prime." % x

Notes: This doesn't bother with even numbers at all, and uses the
square root function of "math."

Any improvements anyone?



More information about the Python-list mailing list