[Shootout-list] nseive.python

Tim digitig@myway.com
Wed, 16 Mar 2005 07:42:50 -0500 (EST)


Naive implementation of nseive benchmark in Python



------------------- snip ---------------------



#!/usr/bin/python

# http://shootout.alioth.debian.org/

#

# Contributed by Tim Rowe



import sys

import math



n = int(sys.argv[1])

for run in range(0, 3):

    m = pow(2, n - run) * 10000

    status = [1] * (m + 1) # include entries for 0 and 1, to avoid

                           # offset calculations in loop

    status[0: 2] = [0, 0] # but 0 and 1 are not prime

    limit = int(math.floor(math.sqrt(m)))

    for i in xrange(2, limit):

        if status[i]:

            for j in xrange(2 * i, m + 1, i):

                status[j] = 0

    print "Primes up to %d: %d" % (m, status.count(1))



_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way your home on the Web - http://www.myway.com