prime number

lostinpython shanna_perkins_02 at hotmail.com
Mon May 30 02:50:44 EDT 2005


It is a homework assignment from a book but not for a class.  I'm
trying to teach my self some basic programming before I have to take it
in college.  If I show enough understanding of the subject, my advisor
will let me forgo Intro. to Programming and go into Intro. to C++.
What civil engineers need with all this programming is beyond me.  We
have to learn another language to program our CADD software, which I
find much easier than this.  But needless to say, I'm stumped on this
problem.  I keep ending up in a never ending loop.

Shanna

Mike Meyer wrote:
> "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