[pypy-svn] r29577 - pypy/dist/pypy/module/_demo

cfbolz at codespeak.net cfbolz at codespeak.net
Sun Jul 2 10:05:56 CEST 2006


Author: cfbolz
Date: Sun Jul  2 10:05:53 2006
New Revision: 29577

Modified:
   pypy/dist/pypy/module/_demo/__init__.py
   pypy/dist/pypy/module/_demo/demo.py
Log:
add a sieve of eratosthenes implementation to the _demo module


Modified: pypy/dist/pypy/module/_demo/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_demo/__init__.py	(original)
+++ pypy/dist/pypy/module/_demo/__init__.py	Sun Jul  2 10:05:53 2006
@@ -5,6 +5,7 @@
 
     interpleveldefs = {
         'measuretime'      : 'demo.measuretime',
+        'sieve'            : 'demo.sieve',
     }
 
     appleveldefs = {

Modified: pypy/dist/pypy/module/_demo/demo.py
==============================================================================
--- pypy/dist/pypy/module/_demo/demo.py	(original)
+++ pypy/dist/pypy/module/_demo/demo.py	Sun Jul  2 10:05:53 2006
@@ -2,7 +2,7 @@
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.rpython.rctypes.tool import ctypes_platform
 from pypy.rpython.rctypes.tool.libc import libc
-import sys
+import sys, math
 from ctypes import *
 
 time_t = ctypes_platform.getsimpletype('time_t', '#include <time.h>', c_long)
@@ -27,3 +27,22 @@
     endtime = time(None)
     return space.wrap(endtime - starttime)
 measuretime.unwrap_spec = [ObjSpace, int, W_Root]
+
+def sieve(space, n):
+    lst = range(2, n + 1)
+    head = 0
+    while 1:
+        first = lst[head]
+        if first > math.sqrt(n) + 1:
+            lst_w = [space.newint(i) for i in range(n)]
+            return space.newlist(lst_w)
+        newlst = []
+        for element in lst:
+            if element <= first:
+                newlst.append(element)
+            elif element % first != 0:
+                newlst.append(element)
+        lst = newlst
+        head += 1
+sieve.unwrap_spec = [ObjSpace, int]
+ 



More information about the Pypy-commit mailing list