[SciPy-user] prime numbers

joep josef.pktd@gmail....
Thu Oct 23 08:53:24 CDT 2008



On Oct 23, 8:28 am, "Walter Mudzimbabwe" <wal...@aims.ac.za> 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]
>
> --
> Walter Mudzimbabwe (Formerly with AIMS)
> University of Western Cape.
> Mathematics Dept,
> Private Bag X17,
> 7535 Bellville,
> RSA
>
> Contact :+27 78 5188402
>          mudzm...@gmail.com
>
> "Those of many tricks take them to the grave".......waltermudz20008
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-u...@scipy.orghttp://projects.scipy.org/mailman/listinfo/scipy-user

return 0 at the end of the for loop

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


More information about the SciPy-user mailing list