[Scipy-svn] r4514 - trunk/scipy/sandbox/mkufunc/examples

scipy-svn@scip... scipy-svn@scip...
Tue Jul 1 08:33:21 CDT 2008


Author: ilan
Date: 2008-07-01 08:33:19 -0500 (Tue, 01 Jul 2008)
New Revision: 4514

Modified:
   trunk/scipy/sandbox/mkufunc/examples/primes.py
Log:
Improved primes example

Modified: trunk/scipy/sandbox/mkufunc/examples/primes.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/primes.py	2008-07-01 07:40:59 UTC (rev 4513)
+++ trunk/scipy/sandbox/mkufunc/examples/primes.py	2008-07-01 13:33:19 UTC (rev 4514)
@@ -2,28 +2,28 @@
 from math import sqrt
 import time
 
-from numpy import arange
-
 from mkufunc.api import mkufunc
 
 
-def is_prime(n):
-    if n < 2:
-        return 0
-    for i in xrange(2, min(n, int(sqrt(n)+2.0))):
-        if n %i == 0:
-            return 0
-    return 1
+def count_primes(N):
+    res = 0
+    for n in xrange(2, N):
+        for i in xrange(2, min(n, int(sqrt(n)+2.0))):
+            if n % i == 0:
+                break
+        else:
+            res += 1
+    return res
 
 
 start_time = time.time()
-assert sum(is_prime(n) for n in xrange(1000000)) == 78498
+assert count_primes(1000000) == 78498
 print 'Python: %.6f sec' % (time.time() - start_time)
 
 
-is_prime = mkufunc(int)(is_prime)
+count_primes = mkufunc(int)(count_primes)
 
 
 start_time = time.time()
-assert is_prime(arange(1000000)).sum() == 78498
+assert count_primes(1000000) == 78498
 print 'Compiled: %.6f sec' % (time.time() - start_time)



More information about the Scipy-svn mailing list