# A (beginner) problem with Cython

Why the following small code generates a problem in Sage-Cython ?

from sage.all import *

cpdef acceptablenumbers(int n, int m):
cdef int i,a,s
cdef list S,l
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return S


while the following code runs very well in Sage-Python :

def acceptablenumbers(n,m):
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return S

edit retag close merge delete

Sort by » oldest newest most voted

I don't think Cython supports generators yet. In any case, changing

s=sum(l[j][1] for j in range(a))


to

s=sum([l[j][1] for j in range(a)])


solves the issue.

more

Thank you Luca, you solved my problem.

( 2013-09-04 18:29:44 +0100 )edit

The yield keyword is available and works, though :-)

( 2013-09-05 05:53:25 +0100 )edit

@Nathann : It works for what ? What do you mean ?

( 2013-09-05 06:16:12 +0100 )edit

Well, I just meant that it works in Cython the same way that it works in Python, which is still somehow recent.

( 2013-09-05 10:41:34 +0100 )edit

@Nathann : I'm sorry for my misunderstanding, but what is your "it" in "it works" ?

( 2013-09-05 11:02:55 +0100 )edit