[Tutor] making lists of prime numbers

c smith illusiontechniques at gmail.com
Thu Sep 15 04:01:16 CEST 2011


hi list, i am trying the MIT opencourseware assignments.
one was to find the 1000th prime.
since this isn't actually my homework, I modified the solution as I would
like to collect lists of primes and non-primes up to N, also some log()
ratio to one comparison.
here is what I came up with on paper:
#!/usr/bin/env python
import sys, os, math

def main(a):
    notprime = []
    primelist = [2,3]
    nprime = sys.argv[1]
    numcheck = 4
    while len(primelist) < nprime:
        for i in range(2,numcheck-1):
            if numcheck % i == 0:
                print numcheck,'is not prime'
                notprime.append(numcheck)
                numcheck += 1
                break
            if i == numcheck and numcheck % i !=0:
                print numcheck, 'is prime'
                primelist.append(numcheck)
                numcheck += 1
                break
    TwotoN = 0
    for j in primelist:
        TwotoN += log(j)
    print 'sum of logs of primes from 2 to', nprime,'is',TwotoN
    print 'the ratio of this sum to 1 is' %f % (float(TwotoN)/nprime)
if __name__=='__main__':
    main(sys.argv[1])

my questions would be:
am I passing arguments from the command line correctly?
this seems to count by twos (going through both 'if statements' maybe?)
I thought the 'break' would send control back to the 'while'
basically, is there an obvious problem here or is it just completely wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110914/8c18eeee/attachment.html>


More information about the Tutor mailing list