Ejemplo n.º 1
0
def Euler263():
    primes = MakePrimeList(1000000)
    eps = []
    base = 840
    sexyTriples = 0
    while len(eps) < 4:
        if (base//84) % 1000000 == 0:
            print('base = {:10}   Sexy triples tested : {:6}'.format(base, sexyTriples))
        for n in [base-20, base+20]:
            if (Miller_Rabin(n-9) and
                not Miller_Rabin(n-7) and
                not Miller_Rabin(n-5) and
                Miller_Rabin(n-3) and
                not Miller_Rabin(n-1) and
                not Miller_Rabin(n+1) and
                Miller_Rabin(n+3) and
                not Miller_Rabin(n+5) and
                not Miller_Rabin(n+7) and
                Miller_Rabin(n+9)):
                sexyTriples += 1
                if (isPractical(n-8, primes) and
                    isPractical(n-4, primes) and
                    isPractical(n, primes) and
                    isPractical(n+4, primes) and
                    isPractical(n+8, primes)):
                    eps.append(n)
                    print('{0:10}\n\tSexy-Triple: {1:50}\n\tPracticals:  {2:60}'.format(n, (n-9, n-3, n+3, n+9), (n-8, n-4, n, n+4, n+8)))
        base += 840
    return sum(eps)
Ejemplo n.º 2
0
def S(N):
    primes = MakePrimeList(N)
    squareDivisors = rootGreatestSquareDivisorList(N)
    s = 0
    for n, a in enumerate(primes):
        if isDigitPowTen(n):
            print n, a, s
        ap = a + 1
        den = squareDivisors[ap]
        num = den + 1
        while True:
            bp = ap * num // den
            cp = ap * num * num // (den * den)
            if cp > N: break
            if Miller_Rabin(bp - 1) and Miller_Rabin(cp - 1):
                s += a + bp + cp - 2
            num += 1

        #for b in primes[n+1:]:
        #    if (b+1)*(b+1) >= (N+1)*(a+1):
        #        break
        #    c = (b+1)*(b+1) // (a+1) - 1
        #    if (a+1)*(c+1) == (b+1)*(b+1) and Miller_Rabin(c):
        #        s += a + b + c
    return s
Ejemplo n.º 3
0
def not2sRelatives(N):
    primes = MakePrimeList(N)
    relatives2 = defaultdict(bool)
    relatives2[2] = True
    count = 0
    for p in primes:
        count += 1
        if count % 100 == 0:
            print '%d/%d\t%d' % (count, len(primes), p)
        if p > 100 and str(p)[:2] == '10':
            continue
        visited = defaultdict(bool)
        shell = [p]
        while shell:
            new_shell = []
            for q in shell:
                visited[q] = True
                if relatives2[q]:  # q is 2's rel ==> p is 2's rel
                    relatives2[p] = True
                    new_shell = []
                    break
                conns = [x for x in getConnections(q) if x < p]
                conns = [x for x in conns if Miller_Rabin(x)]
                conns = [x for x in conns if not visited[x]]
                new_shell.extend(conns)
            shell = new_shell
            #print shell, visited[101]
    return [p for p in primes if not relatives2[p]]
Ejemplo n.º 4
0
def D(a, b, k):
    ret = 0
    for p in range(a, a + b):
        if not Miller_Rabin(p):
            continue
        ret += pow(k - 1, p - 2, p)
    return ret
Ejemplo n.º 5
0
def Euler487(k,n,x,y):
    ans = 0
    for p in range(x,x+y+1):
        if not Miller_Rabin(p):
            continue
        s = S(k,n,p)
        print p,s
        ans += s
    return ans
Ejemplo n.º 6
0
def B(x, y, n):
    ans = 0
    for p in xrange(x, x + y + 1):
        #if p%10000 == 0:
        #print p-x, ans
        if not Miller_Rabin(p):
            continue
        ans += x2a(xnModP(n, p), p)
    return ans
Ejemplo n.º 7
0
def Euler516(N):
    bhn = [x for x in base_hamming_nums(1, N)]
    ret = sum(bhn)
    #ret_vec = []
    #ret_vec.extend(bhn)
    hamming_primes = [x + 1 for x in bhn if Miller_Rabin(x + 1) and x > 5]
    hamming_primes.sort()

    for r in range(1, len(hamming_primes)):
        for c in combProdLessThan(hamming_primes, r, N):
            #print c
            #ret_vec.extend([x for x in base_hamming_nums(prod(c),N)])
            ret += sum([x for x in base_hamming_nums(prod(c), N)])
    return ret  #, ret_vec
Ejemplo n.º 8
0
def g(N):
    ps = MakePrimeList(int((2*N+2)**(0.5)))
    n = 9
    n_last = 9
    while n <= N:
        #print n
        z = 2*n-1
        if Miller_Rabin(z):
            k = (z-3)/2
        for p in ps:
            if z%p == 0:
                k = (p-3)/2
                break
        n_last = n
        n += k + 1
    return 2*n_last + N
Ejemplo n.º 9
0
def S(n, pMOD):
    r = int(n**0.5) + 1
    primes = MakePrimeList(r)
    eligiblePrimePowers = []
    for p in primes:
        pp = 1
        pS = 0
        while pp <= n:
            pS += pp
            if pS % pMOD == 0:
                eligiblePrimePowers.append((pp, p))
            pp *= p
    # Check even multiples of pMOD
    start = (r // (2 * pMOD) + 1) * (2 * pMOD)
    for x in range(start, n + 1, 2 * pMOD):
        if Miller_Rabin(x - 1):
            eligiblePrimePowers.append((x - 1, x - 1))
    eligiblePrimePowers.sort()
    print len(eligiblePrimePowers)
Ejemplo n.º 10
0
    def partialSumOverPrimes(self, k, v, m):  # W
        self.psopCalled += 1
        if (k, v, m) in self.partialSumOverPrimesCache:
            return self.partialSumOverPrimesCache[(k, v, m)]

        if m < 2:
            return self.evaluatePolynomial(self.faulhaber[k], v) - 1

        if v < m * m: return self.sumOverPrimes(k, v)
        if not Miller_Rabin(m):
            p = self.primes[bisect(self.primes, m) - 1]
            return self.partialSumOverPrimes(k, v, p)

        # Pre-compute some values to avoid hitting recursion depth max
        #l = m-1
        #while l > 2 and (k,v,l) not in self.partialSumOverPrimesCache:
        #    l -= 1
        #for j in range(l, m):
        #    self.partialSumOverPrimes(k, v, j)
        #
        #ans = self.partialSumOverPrimes(k, v, m-1)
        #ans -= pow(m, k, self.MOD) * self.partialSumOverPrimes(k, v//m, m-1)
        #ans += pow(m, k, self.MOD) * self.partialSumOverPrimes(k, m-1, m-1)
        #ans %= self.MOD

        p = m
        ans = self.partialSumOverPrimes(k, v, 1)
        for qix, q in enumerate(self.primes):
            if q > p: break
            if (v == self.N and ddPow10(qix, 2)):
                print 'sop', v, k, qix, q, len(self.partialSumCache), len(
                    self.partialSumOverPrimesCache)
            ans -= pow(q, k, self.MOD) * (self.partialSumOverPrimes(
                k, v // q, q - 1) - self.partialSumOverPrimes(k, q - 1, q - 1))
            ans %= self.MOD

        self.partialSumOverPrimesCache[(k, v, m)] = ans
        return ans
Ejemplo n.º 11
0
    def partialSum(self, k, n, m):
        self.psCalled += 1
        if (k, n, m) in self.partialSumCache:
            self.psCacheHits += 1
            return self.partialSumCache[(k, n, m)]
        if m >= n:
            return self.S(k, n) - 1
        if m < 2:
            return 0
        if m == 2:
            return (pow(2, k, self.MOD) * intLog(n, 2)) % self.MOD

        if not Miller_Rabin(m):
            p = self.primes[bisect(self.primes, m) - 1]
            return self.partialSum(k, n, p)

        # Precompute some values to avoid hitting recursion depth limits
        pix = bisect(self.primes, m) - 1
        p = self.primes[pix]
        #pLess = self.primes[pix-1]
        #ix = pix-1
        #if (n == self.N and dPow10(pix)) or n == 10**12: #dPow10(pix):
        #    print n, k, pix, p, self.psCalled, len(self.partialSumCache), self.psopCalled, len(self.partialSumOverPrimesCache)
        #while ix > 0 and (k, n, self.primes[ix]) not in self.partialSumCache:
        #    ix -= 1
        #for jx in range(ix,pix):
        #    self.partialSum(k, n, self.primes[jx])
        #
        #ans = intLog(n, p)
        #for e in range(1, intLog(n, p)+1):
        #    ans += self.partialSum(k, n//pow(p,e), pLess)
        #    ans %= self.MOD
        #ans *= pow(p,k,self.MOD)
        #ans += self.partialSum(k, n, pLess)
        #ans %= self.MOD

        ans = 0
        r = intRoot(n)
        for qix, q in enumerate(self.primes):
            if q > p or q > r: break
            if (n == self.N and dPow10(qix)) or n == 10**12:
                print 'sum', n, k, qix, q, len(
                    self.partialSumCache
                ), self.psCalled, self.psCacheHits, len(
                    self.partialSumOverPrimesCache)
            partAns = intLog(n, q)
            if not q == 2:
                for e in range(1, intLog(n, q) + 1):
                    partAns += self.partialSum(k, n // (q**e), q - 1)
                    partAns %= self.MOD
            ans += pow(q, k, self.MOD) * partAns
            ans %= self.MOD
        if q <= r:
            self.partialSumCache[(k, n, m)] = ans
            return ans

        for j in range(n // p, n // r + 1):
            ans += self.S(k,
                          j) * (self.sumOverPrimes(k, min(p, max(r, n // j))) -
                                self.sumOverPrimes(k, max(r, n // (j + 1))))
            ans %= self.MOD

        #for q in self.primes[qix:]:
        #    if q > p: break
        #    if (n == self.N and dPow10(qix)) or n == 10**12:
        #        print 'sum', n, k, qix, q, len(self.partialSumCache), self.psCalled, self.psCacheHits, len(self.partialSumOverPrimesCache)
        #    partAns = intLog(n,q)
        #    if not q == 2:
        #        for e in range(1, intLog(n,q)+1):
        #            partAns += self.partialSum(k, n//(q**e), q-1)
        #            partAns %= self.MOD
        #    ans += pow(q, k, self.MOD) * partAns
        #    ans %= self.MOD

        self.partialSumCache[(k, n, m)] = ans
        return ans
Ejemplo n.º 12
0
def getPrimeConnections(p):
    conns = getConnections(p)
    conns = [q for q in conns if Miller_Rabin(q)]
    return conns
Ejemplo n.º 13
0
        return 0
    if k > n // 2:
        k = n - k
    num = factorials[n]
    den = (factorials[k] * factorials[n - k]) % MOD
    ##    try:
    ##        return memo[(n,k)]
    ##    except KeyError:
    ##        pass
    ##    num, den = 1,1
    ##    for kk in range(1,k+1):
    ##        num *= n-kk+1
    ##        den *= kk
    ##        num %= MOD
    ##        den %= MOD
    return (num * pow(den, MOD - 2, MOD)) % MOD


##    memo[(n,k)] = (num * pow(den,MOD-2,MOD)) % MOD
##    return memo[(n,k)]

if __name__ == '__main__':
    ans = 0
    for p in range(MIN + 1, MAX):
        if Miller_Rabin(p):
            g = G(p)
            ans += g
            ans %= MOD
            print p, g, ans
    print ans