[SciPy-user] prime numbers
Adolph J. Vogel
ajvogel at tuks.co.za
Thu Oct 23 09:57:53 EDT 2008
On Thursday 23 October 2008 14:28:40 Walter Mudzimbabwe wrote:
> can anybody help me figure out why the following program cannot produce
> primes upto 10.
> --------------------------------------------------
> from scipy import *
>
> def isdivisible(n,listt):
> for i in range(len(listt)):
> if (n%listt[i]==0):
> return 1
> else:
> return 0
>
> def primes_upto(m):
> u=[1,2]
> for i in range(3,m+1):
At this point your only passing the last element of u, which in this case is
two. So in isdivisibile() your only checking to see if it is divisible by
two. You need to check and see whether or not its divisible by the divisors
smaller then two as well.
Your code is also very complicated for such an simple task.
def isdivisible(p):
for d in range(1,p):
if p % d == 0:
return False
return True
primes = []
def prime_upto(m):
for i in range(3,m+1,2):
if isdivisible(i): primes.append(i)
--
Adolph J. Vogel BEng(Mech)
More information about the SciPy-User
mailing list