Ask Your Question
0

How to compute on Cython mode with 64-bit int?

asked 2018-10-05 04:52:59 -0500

updated 2018-10-05 05:10:10 -0500

The computation is on SageMath 8.3, on a 64-bit computer.
If the integers are less than $2^{31}$ everything is alright:

sage: %time champions(2**30,2**30+10)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 2.15 ms

But if the integers are greater than $2^{31}$, there is a problem OverflowError: value too large to convert to int.

sage: champions(2**31,2**31+10)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-7-2a762f4cafd8> in <module>()
----> 1 champions(Integer(2)**Integer(31),Integer(2)**Integer(31)+Integer(10))

/home/sage/.sage/temp/LAPTOP-7O5QV19T/9856/spyx/_home_sage_SAGE_EulerRH_spyx/_home_sage_SAGE_EulerRH_spyx_0.pyx in _home_sage_SAGE_EulerRH_spyx_0.champions()
     12         return len(list(factor(n)))
     13
---> 14 cpdef champions(int m1, int m2):
     15         cdef int n,o,s,p,pr
     16         cdef float a,c

OverflowError: value too large to convert to int

Here is the code:

from sage.all import *

cpdef g(float x):
    return x/(exp(float(euler_gamma))*ln(ln(x)))

cpdef omega(int n):
    return len(list(factor(n)))

cpdef champions(int m1, int m2):
    cdef int n,o,s,p,pr
    cdef float a,c
    a=1.7683358; s=2; p=3; pr=6; n=m1
    while n<=m2:
        if mod(n,10**7)==0:
            print(n)
        if n>pr:
            p=next_prime(p); pr*=p; s+=1
        o=omega(n)
        if n<>pr:
            c=(euler_phi(n)/g(float(n)))**(1/float(s-o))
            if c>a:
                a=c
                print([n,a,factor(Integer(pr)/Integer(n))])
        n+=1
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2018-10-05 11:11:50 -0500

tmonteil gravatar image

The int you declare in your cython function is a C int, hence it is encoded on 32 bits. If you want to use integers encoded on 64 bits, you need to use long.

edit flag offensive delete link more

Comments

Thanks! Is it as quick to compute with long than with int?

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2018-10-05 11:25:23 -0500 )edit
1

I would say that the difference is negligible compared to the computation of euler_phi, factor or next_prime.

tmonteil gravatar imagetmonteil ( 2018-10-05 12:17:19 -0500 )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

1 follower

Stats

Asked: 2018-10-05 04:52:59 -0500

Seen: 45 times

Last updated: Oct 05