[SciPy-user] prime numbers

Etienne Gaudrain et.gaudrain@free...
Thu Oct 23 08:40:53 CDT 2008


Hi,

This is probably not the best place to post your question...
However, this version should work:

--------------------------------------------------
from scipy import *

def isdivisible(n,listt):
   for i in range(len(listt)):
       if (n%listt[i]==0):
           return 1
   return 0

def primes_upto(m):
   u=[1,2]
   for i in range(3,m+1):
        if (isdivisible(i,u[1:])==0):
           u.append(i)
   return u

print primes_upto(10)
-----------------------------------------------------

Note that this is not the best algorithm. Google "primes sieve".

-Etienne




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):
>         if (isdivisible(i,u[1:])==0):
>            u.append(i)
>    return u
>
> print primes_upto(10)
> -----------------------------------------------------
> it's output is:
>
> [1, 2, 3, 5, 7, 9]
>
>
>
>   


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Etienne Gaudrain
Centre for the Neural Basis of Hearing
Department of Physiology, Development and Neuroscience
University of Cambridge
Downing Street
Cambridge CB2 3EG
UK
Phone: +44 (1223) 333 859 office
Fax: +44 (1223) 333 840 department
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the SciPy-user mailing list