Exemplo n.º 1
0
def count_circular_primes(n):
    l = mtools.sieve_primes(n)
    count = 0
    for i in l:
        if is_circular(i, l):
            count = count + 1
    return count
Exemplo n.º 2
0
def max_sum_primes(n):
    l = mtools.sieve_primes(n)
    lim = len(l)
    sum_max = l[lim-1]
    all_max = 0
    s_all_max = 0
    for i in range(lim-2):
        s_max = max = 0
        np = 2
        sum = l[i] + l[i+1]
#        print 'i=%s' % i
        j = i + 2
        no_add = True
        while j < lim:
            while (j < lim) and (np <= all_max) and (sum <= sum_max):
                sum = sum + l[j]
                j = j + 1
                np = np + 1
            if (sum > sum_max) or (j > lim):
                if no_add:
                    return s_all_max
                break
            if mtools.is_prime(sum):
                max = np
                s_max = sum
#                print 'max=%s s_max=%s' % (max, s_max)
            sum = sum + l[j]
            j = j + 1
            np = np + 1
            no_add = False
        if max > all_max:
            all_max = max
            s_all_max = s_max
#            print all_max, s_all_max
    return s_all_max
Exemplo n.º 3
0
def solve():
    l = [str(x) for x in mtools.sieve_primes(1000000)]
    for i in range(5,8):
        dls = mtools.comb(range(i-1), 3)
        for l2 in dls:
            l2.sort()
            print i, l2
            n, p = detect(i, l, l2)
            if n == 8:
                return p
Exemplo n.º 4
0
def build_arr(M, N):
    pm = mtools.sieve_primes(M+1)
    res = []
    for p in pm:
        v = 1
        l = []
        while v <= N:
            l.append(v)
            v *=p 
        res.append(l[:])
    return res
Exemplo n.º 5
0
def count_semi_primes(n):
    pt = mtools.sieve_primes(n/2 + 1)
    i = 0
    cur = len(pt) - 1
    cnt = 0
    while i <= cur:
        p = pt[i]
        while pt[cur]*p >= n:
            cur -=1
        cnt += (cur-i+1)
        i += 1
    return cnt        
Exemplo n.º 6
0
def solve():
    ps = mtools.sieve_primes(10**6)
    n = 10**9
    res = []
    for p in ps:
        if p < 7:
            continue
        if pow(10, n, p) == 1:
            res.append(p)
        if len(res) == 40:
            print res
            return sum(res)
Exemplo n.º 7
0
def sum_n(lim):
    s = 0
    _ps = mtools.sieve_primes(lim+1)
    ps = _ps[2:]
    for i in range(len(ps)-1):
        p1 = ps[i]
        if p1 > 10**6:
            return s
        p2 = ps[i+1]
        n = get_n(p1, p2)
        s += n
    return s
Exemplo n.º 8
0
def sp_prime(max):
    d = {}
    cnt = 0
    l = mtools.sieve_primes(7072)
    la = [x for x in l if x < 85]
    lb = [x for x in l if x < 400]
    for a in la:
        for b in lb:
            for c in l:
                v = a*a*a*a + b*b*b + c*c
                if v < max and not d.has_key(v):
                    d[v] = True
                    cnt = cnt + 1
                if v >= max:
                    break
    return cnt
Exemplo n.º 9
0
def solve():
    l = [x for x in mtools.sieve_primes(10000) if len(str(x)) == 4]
    i = 0
    while i < len(l):
        s = set(l)
        lp = [int(x) for x in mtools.permute(list(str(l[i])))]
        si = set(lp) & s
        li = list(si)
        li.sort()
        for j in range(len(li)-2):
            for k in range(j+1,len(li)-1):
                d = li[k] - li[j]
                if (li[k]+d) in li:
                    if li[j] != 1487:
                        return str(li[j]) + str(li[k]) + str(li[k]+d)
        l = list(s - si)
Exemplo n.º 10
0
def one_angle_triangle(lim):
    rlim = lim / 2
    l = init_list(rlim+1)
    plist = mtools.sieve_primes(rlim/6+1)
    #print rlim, plist
    for t in range(6,rlim+1):
        if not l[t]:
            mark_similar(l, t, plist, rlim)
            continue
        up = int(math.sqrt(t))
        if up*up != t:
            up = up + 1
        down = int(math.sqrt(t/2)) + 1
        #print up, down
        for m in range(down, up):
            if t % m == 0:
                #print t, m
                n = t/m - m
                a = m*m - n*n
                b = 2*m*n
                if a > b:
                    tmp = a
                    a = b
                    b = tmp
                if l[t] != (0, 0):
                    if l[t] != (a, b):
                        #more than 1 solution
                        l[t] = False
                        break
                else:
                    #found a foundational solution
                    l[t] = (a, b)
        mark_similar(l, t, plist, rlim)
    l2 = [i for i in range(len(l)) if l[i] and l[i] != (0, 0)]
#    print l2, l
    return len(l2)
Exemplo n.º 11
0
def test():
    l = [9, 15, 21, 25, 27, 33]
    for x in l:
        assert separatable(x, mtools.sieve_primes(100))
Exemplo n.º 12
0
def solve():
    l = mtools.sieve_primes(1000000)
    l2 = list(set(range(33, 1000000, 2)) - set(l))
    for i in l2:
        if not separatable(i, l):
            return i
Exemplo n.º 13
0
def solve():
    l = mtools.sieve_primes(10000)
    lp = []
    layer = 5
    return sum(find_comb_primes(lp, l, 0, len(l)-layer+1, layer))
Exemplo n.º 14
0
def solve():
    # guess upper limit
    l = mtools.sieve_primes(10000000)
    l = [x for x in l if pandigital(x)]
    return l[len(l)-1]
Exemplo n.º 15
0
def sum_prime_ways(n):
    l = mtools.sieve_primes(n)
    l.reverse()
    ret = sum_prime_ways_rec(n, l, 0, {})
    #print 'sum_prime_ways(%d) = %d' % (n, ret)
    return ret
Exemplo n.º 16
0
def solve():
    l = mtools.sieve_primes(1000000)
    l = [x for x in l if x > 10 and is_truncatable(x)]
    print l
    assert len(l) == 11
    return sum(l)
Exemplo n.º 17
0
def test():
    l = mtools.sieve_primes(1000)
    assert is_circular(197, l)
    assert 13 == count_circular_primes(100)
Exemplo n.º 18
0
'''
Created on 2012-9-18

@author: chrisliu
'''

import mtools
import unittest
import time

primes = mtools.sieve_primes(100)

def get_n(l):
    l2 = [primes[i]**x for (i, x) in enumerate(l)]
    return mtools.mul(l2)    

def inc_elem(l, i):
    l2 = list(l)
    l2[i] += 1
    return l2

def hashable(l):
    return ','.join([str(x) for x in l])

def expand_states(l):
    idx = 0
    new_states = []
    new_states.append(inc_elem(l, idx))
    for i in range(len(l)-1):
        if l[i+1] < l[i]:
            idx = i+1