Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One bottleneck comes from working with the symbolic ring. The following code is some hundred times faster (may be still not fast enough ;-) )

import math
def count_2017(bound=100000):
    cpt = 0
    for n in range (1,bound):
        if int(RR(math.sqrt(n)).frac()*10000) == 2017:
            cpt += 1
    return cpt

One bottleneck comes from working with the symbolic ring. The following code is some hundred times faster (may be still not fast enough ;-) )

import math
def count_2017(bound=100000):
    cpt = 0
    for n in range (1,bound):
        if int(RR(math.sqrt(n)).frac()*10000) == 2017:
            cpt += 1
    return cpt

Update: Using cython makes the code another ten times faster, for example:

fits_2017 = cython_lambda("double x", "bool(int(10000*(x-int(x))) == 2017)")
f = cython_lambda("int n", "sqrt(n)")

def count_2017(bound=100000):
    cpt = 0
    for n in range(1,bound):
        if fits_2017(f(n)):
            cpt += 1
    return cpt

count_2017(10^7)