Exemple #1
0
def solve():
    print('start')
    o = 10
    for p in primes.primes_sieve(27000):
        if p == 2 or p == 5:
            continue
        while p > o:
            o *= 10 
        for g in groups:
            if ok(g, (p, o)):
                newg = list(g)
                newg.append((p, o))
                groups.append(newg)
                if (len(newg)) >= 5:
                    print(newg, sum(x[0] for x in newg))
Exemple #2
0
from primes import primes_sieve
import itertools

is_prime = primes_sieve(9999)


def check_sequence(a):
    prime_set = [
        int("".join(x)) for x in itertools.permutations(str(a))
        if is_prime[int("".join(x))]
    ]

    l = len(prime_set)
    if l >= 3:
        for i in range(l):
            for j in range(i + 1, l):
                for k in range(j + 1, l):
                    a = prime_set[k] - prime_set[j]
                    if a == prime_set[j] - prime_set[i] and a > 0:
                        print prime_set[i], prime_set[j], prime_set[k]
                        return True
    return False


for i in range(1488, 9999):
    check_sequence(i)
Exemple #3
0
from primes import primes_sieve, primes_sieve_gen

limit = 1000000
primes = [x for x in primes_sieve_gen(10000)]
is_prime = primes_sieve(1000000)
squares = [i * i for i in range(1, 100)]

first = [i for i in range(2, limit) if not i % 2 == 0 and not is_prime[i]]
second = [False] * limit
for i in first:
    second[i] = True

for i in primes:
    for j in squares:
        second[i + 2 * j] = False

print second.index(True)
Exemple #4
0
def has_at_least_3_same_digits(number):
    d = {}
    for letter in str(number):
        if letter not in d.keys():
            d[letter] = 1
        else:
            d[letter] += 1
    return max(d.itervalues()) >= 3


limit = 1000000

primes = [x for x in primes_sieve_gen(limit)]
primes = primes[primes.index(56003) - 1:]
is_prime = primes_sieve(limit)

replacement_digits = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)

def calculate():
    found = False
    already_checked = []
    counter = 10
    while not found:
        prime = primes.pop(0)
        if not has_at_least_3_same_digits(prime):
            continue
        print prime
        for n_of_digits in xrange(3, len(str(prime)), 3):
            for sequence in set(["".join(c) for c in
                                 itertools.combinations('0123456789'[:len(str(prime))-1], n_of_digits)]):
Exemple #5
0
#!/usr/bin/python

import primes

p = list(primes.primes_sieve(10**6))

def isPrime(n):
    if n <= 1: 
        return False
    if n % 2 == 0:
        return n == 2
    for i in xrange(100000):
        if p[i] * p[i] > n:
            return True
        if n % p[i] == 0:
            return False


sss = 0
for d in xrange(0, 10):
    print d
    o = int(str(d) * 10)
    if isPrime(o):
        sss += o
        print o
        continue

    found = set()
    for a in xrange(0, 10):
        for pos in xrange(0, 10):
            c = [d] * pos + [a] + [d] * (9 - pos)
Exemple #6
0
#!/usr/bin/python3

import primes

p = list(primes.primes_sieve(8000000))

def t(n):
    return n*(n+1)//2

assert t(8) == 36

def s(n):
    start = t(n - 3) + 1
    end = t(n + 2) + 1
    m = [1] * (end - start)
    neighbours = [0] * (end - start)

    s = 0
    filterPr(start, end, m, p)
    #print(1, sum(1 for x in m if x > 0))
    filterNeigh(n, start, end, m, neighbours)
    #print(2, sum(1 for x in m if x > 0))
    for x in range(t(n - 1) + 1, t(n) + 1):
        if m[x - start] >= 2 or (m[x - start] == 1 and m[neighbours[x - start] - start] >= 2):
                s += x
    return s

def filterPr(start, end, m, pr):
    for p in pr:
        if p*p > end:
            break
Exemple #7
0
#!/usr/bin/python

import primes
import itertools

p = list(primes.primes_sieve(120000))

def factors(n):
    if n <= 1:
        return []
    f = 0
    r = []
    while p[f] <= n:
        isf = False
        while n % p[f] == 0:
            isf = True
            n //= p[f]
        if isf: r.append(p[f])
        f += 1
    return r

def solve(limit, cnt):
    a = [True] * (limit + 1)
    a[0] = a[1] = False
    cnt -= 1
    while True:
        for i, v in enumerate(a):
            if v:
                c = factors(i)
                break
        #print c
Exemple #8
0
#!/usr/bin/python3

import primes

pr = list(primes.primes_sieve(1000))

a = {}
def calc(s, m): # how many ways to get sum s using terms less than or equal to m
    if s < 0 or s == 1:
        return 0
    if s == 0:
        return 1
    if (s,m) in a:
        return a[(s,m)]
    else:
        t = 0
        for p in pr:
            if p > m or p > s:
                break
            t += calc(s - p, p)
        a[(s,m)] = t
        return a[(s, m)]

assert calc(10, 10) == 5

n = 2
while calc(n, n) <= 5000:
    n += 1
print(n)
assert n == 71