Ask Your Question
1

A (beginner) problem with Cython

asked 2013-09-04 17:48:34 +0100

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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2013-09-04 18:22:36 +0100

Luca gravatar image

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.

edit flag offensive delete link more

Comments

Thank you Luca, you solved my problem.

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-04 18:29:44 +0100 )edit

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

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

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

Sébastien Palcoux gravatar imageSébastien Palcoux ( 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.

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

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

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-05 11:02:55 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-09-04 17:48:34 +0100

Seen: 341 times

Last updated: Sep 04 '13