示例#1
0
def S(n):
    r = intRoot(intRoot(n)) + 1
    # Use Stern-Brocot to generate coprime pairs
    ans = fromPrimitive(1, 1, n)
    sb = [(0, 1, 1, 1)]
    while len(sb) > 0:
        n1, d1, n2, d2 = sb.pop()
        if d1 + d2 <= r:
            ans += fromPrimitive(n1 + n2, d1 + d2, n)
            sb.append((n1, d1, n1 + n2, d1 + d2))
            sb.append((n1 + n2, d1 + d2, n2, d2))
    return ans
示例#2
0
def S2(n):
    r = intRoot(intRoot(n)) + 1
    # Iterate to find coprime pairs -  not sure which of these two is faster.
    ans = 0
    for x in range(1, r + 1):
        for y in range(1, x + 1):
            if gcd(x, y) != 1: continue
            a = x**2 * (x + y)**2
            k = n // a
            if k == 0: break
            b = y**2 * (x + y)**2
            c = x * x * y * y
            ans += T(k) * (a + b + c)
    return ans
示例#3
0
 def __init__(self, N, talk=False):
     self.talk = talk
     self.N = N
     self.r = intRoot(N)
     self.primes = MakePrimeList(self.r)
     self.mobius = Mobius(self.r)
     self.isSquareFree = self.makeSquareFree(self.r)
     self.factCache = {}
     self.sfCache = {}
示例#4
0
    def countSquareFree(self, n):
        if n in self.sfCache:
            return self.sfCache[n]

        ans = sum(
            self.mobius(x) * (n // (x * x)) for x in range(1,
                                                           intRoot(n) + 1))
        self.sfCache[n] = ans
        return ans
示例#5
0
 def sumTotient(self, n):
     if n == 1: return 0
     if n == 2: return 1
     r = intRoot(n)
     ans = 0
     for k in range(1, r + 1):
         ans += self.mobius(k) * T(n // k)
         ans += T(k) * (self.mertens(max(r, n // k)) -
                        self.mertens(max(r, n // (k + 1))))
     return ans
示例#6
0
    def countFactorizations(self, n, fMin=2):
        if (n, fMin) in self.factCache:
            return self.factCache[(n, fMin)]

        ans = self.countSquareFree(n) - self.countSquareFree(fMin - 1)
        for f in range(fMin, intRoot(n) + 1):
            if self.isSquareFree[f]:
                ans += self.countFactorizations(n // f, f)
        self.factCache[(n, fMin)] = ans

        if self.talk and fMin == 2:
            print 'S({0}) = {1}'.format(n, ans)
        return ans
示例#7
0
 def makeSquareFree(self, r):
     sf = [True] * (r + 1)
     for x in range(2, intRoot(r) + 1):
         for y in range(x * x, r + 1, x * x):
             sf[y] = False
     return sf
示例#8
0
 def __init__(self, n):
     self.mertens = Mertens(n)
     self.mobius = Mobius(intRoot(n))