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

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

Sort by » oldest newest most voted

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.

more

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

( 2018-10-05 18:25:23 +0200 )edit
1

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

( 2018-10-05 19:17:19 +0200 )edit