prime number

Mike Meyer mwm at mired.org
Sun May 29 23:43:32 EDT 2005


"lostinpython" <shanna_perkins_02 at hotmail.com> writes:
> I'm having trouble writing a program that figures out a prime number.
> Does anyone have an idea on how to write it?  All I know is that n > 2
> is prim if no number between 2 and sqrt of n (inclusivly) evenly
> divides n.

How about this (untested):

import sys
import subprocess

primes = subprocess.Popen(["primes", sys.argv[1], sys.argv[1]],
                        stdout=subprocess.PIPE).stdout.readlines()
if primes:
   print sys.argv[1], "prime"
else:
   print sys.argv[1], "not prime"

Seriously, this sounds like a homework assignment. Google for "sieve
of eratosthenes". The BSD "primes" program I used in the above is an
implementation of that in C.

If it isn't a homework assignment, and you're honestly in such, then
you should know there's been a lot of research in this area, because
primes are important in cryptographic applications. Once again, google
is a good place to start on looking for recent research on the subject.

   <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list