Ask Your Question
0

A (beginner) problem with Cython

asked 2013-09-04 10:48:34 -0600

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 11:22:36 -0600

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 11:29:44 -0600 )edit

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

Nathann gravatar imageNathann ( 2013-09-04 22:53:25 -0600 )edit

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

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-04 23:16:12 -0600 )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 03:41:34 -0600 )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 04:02:55 -0600 )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 10:48:34 -0600

Seen: 88 times

Last updated: Sep 04 '13