Ask Your Question

Revision history [back]

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