Ask Your Question

Revision history [back]

The statement

result += (fact - j*(<int>t))

will cast t to an int (dropping the fractional part like floor does) without calling floor. In the annotated cython code this change reduces the "yellowness" of that line. I'm not sure why it's yellow at all though, it seems like this statement should translate easily to pure C code.

If you want to use the C math library version of floor I think you need an appropriate include statement.

The statement

result += (fact - j*(<int>t))

will cast t to an int (dropping the fractional part like floor does) without calling floor. In the annotated cython code this change reduces the "yellowness" of that line. I'm not sure why it's yellow at all though, it seems like this statement should translate easily to pure C code.

If you want to use the C math library version of floor I think you need an appropriate include statement.


Edit:

If you use the @cython.cdivision(True) decorator, cython will not add exception checking for division by zero, this allows the division statement to be converted to one line of pure C code. In your code j is never zero, so this is okay. Also, I assume you want fact/j to be a fraction, not an integer, so in C you need to cast fact and j to a floating point type before the division, otherwise they are divided as integers with an integer result (the floor of the fraction).

Here is new code with the changes:

%cython
import cython

@cython.cdivision(True)
def primeish(int n):
    cdef int fact
    cdef int result
    cdef int j
    cdef double t
    if n==1:
        return 0
    elif n==2:
        return 1
    elif n==3:
        return 2
    else:
        result = -1
        fact = 1
        for j in range(3,n+1):
            fact = fact*(j-2)
            t = <double>fact / <double>j
            result += fact - <int>t*j
        return result

The statement

result += (fact - j*(<int>t))

will cast t to an int (dropping the fractional part like floor does) without calling floor. In the annotated cython code this change reduces the "yellowness" of that line. I'm not sure why it's yellow at all though, it seems like this statement should translate easily to pure C code.

If you want to use the C math library version of floor I think you need an appropriate include statement.


Edit:

If you use the @cython.cdivision(True) decorator, cython will not add exception checking for division by zero, this allows the division statement to be converted to one line of pure C code. In your code j is never zero, so this is okay. Also, I assume you want can skip the double t entirely if you just write fact/j to be a fraction, not an integer, so in C you need to cast fact and j to a floating point type before the division, otherwise they . Since both are divided integers, the division occurs as integers with an integer and the result (the is the floor of the fraction).rational number fact/j.

Here is new code with the changes:

%cython
import cython

@cython.cdivision(True)
@cython.cdivision(True)    # turn division by zero checking off
def primeish(int n):
    cdef int fact
    cdef int result
    cdef int j
    cdef double int t
    if n==1:
        return 0
    elif n==2:
        return 1
    elif n==3:
        return 2
    else:
        result = -1
        fact = 1
        for j in range(3,n+1):
            fact = fact*(j-2)
            t = <double>fact fact / <double>j
j    # integer division
            result += fact - <int>t*j
t*j
        return result