Ask Your Question

Revision history [back]

Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:

def my_function(k=51):
    L = []
    for n in range(1, k):
        a = n**2 + 1
        b = (n+1)**2 + 1
        c = 2*n
        primes = sum(1 for p in prime_range(a, b))
        # QQ(...) converts the argument to a rational number
        L.append(QQ(nprimes/c).denominator())
    return L

Then you can run lcm(L).

Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:

def my_function(k=51):
    # L = list in which to collect the answers
    L = []
    for n in range(1, k):
        a = n**2 + 1
        b = (n+1)**2 + 1
        c = 2*n
        primes = sum(1 for p in prime_range(a, b))
        # QQ(...) converts the argument to a rational number
        # L.append(...) appends the argument to L
        L.append(QQ(nprimes/c).denominator())
    return L

Then you can run lcm(L).

Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:

def my_function(k=51):
    # L = list in which to collect the answers
    L = []
    for n in range(1, k):
        a = n**2 + 1
        b = (n+1)**2 + 1
        c = 2*n
        primes nprimes = sum(1 for p in prime_range(a, b))
        # QQ(...) converts the argument to a rational number
        # L.append(...) appends the argument to L
        L.append(QQ(nprimes/c).denominator())
    return L

Then you can run lcm(L).