a better prime number generator
Alves, Carlos Alberto - Coelce
calves at coelce.com.br
Tue Oct 23 10:26:46 EDT 2001
Why not try a variation from basic example in Python Tutorial by Guido van
Rossum?!
def getPrimesTill(x):
primes=[2]
for i in range(3,x,2):
for x in range(2,i):
if i%x==0:
break
else:
primes.append(i)
return primes
Carlos Alberto
COELCE/DPRON-Departamento de Projetos e Obras Norte
Fone: 677- 2228
e-mail: calves at coelce.com.br
\|||/
(o o)
--ooo0-(_)-0ooo--
-----Original Message-----
From: dhillon_rs at rediffmail.com [mailto:dhillon_rs at rediffmail.com]
Sent: Sunday, October 21, 2001 2:40 PM
To: python-list at python.org
Subject: a better prime number generator
hi all,
I have just been introduced to pyton and find it a very good ( takes
off all the bookwork ) language. I was trying to develope a ast prime
number generator. I designed the following algo. Can you please
suggest a faster one or modifications to this only to make it faster
#the following algo returns all the primes below x
def getPrimesTill(x):
a = range(2,x)
c = [2]
i = 0
j = 0
foundone = 1
while i < len(a):
while j < len(c) and (c[j] < (.5 * a[i]) ) and foundone ==
1:
if a[i]%c[j] == 0:
foundone = 0
j = j + 1
if foundone == 1 and i > 0:
c.append(a[i])
j = 0
foundone = 1
i = i + 1
return c
roop
--
http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011023/23e7d966/attachment.html>
More information about the Python-list
mailing list