Ejemplo n.º 1
0
def main(max_ = 30000):
    # 各素数に対応する点の次数を管理して、次数が5未満の点は切り捨てたほうが良いかも.
    ans = []
    primes = intlib.primes(max_)
    adj = defaultdict(set)
    
    for p in primes:
        for q in (q for q in primes if q > p):
            if is_pair(p, q):
                adj[p].add(q)
                adj[q].add(p)
    
    adj = {p: s for p, s in adj.items() if len(adj[p]) > 4} #次数4以下の点を削除
    adj = {p: {q for q in ls if q in adj} for p, ls in adj.items()} #枝を更新
    print(len(adj), sum((len(s) for s in adj.values()))/len(adj))
    
    keys = sorted(adj.keys())
    ans = float('inf')
    for p in keys:
        if p > ans: break # pが暫定解を超えたら終了
        adj_p = sorted([x for x in adj[p] if x > p])
        for q in adj_p:
            if len(adj[p] & adj[q]) < 3: continue #pとqの共通隣接点は3個以上必要
            common_adj = sorted([x for x in adj[p] & adj[q] if x > q])
            for r, s, t in itertools.combinations(common_adj, 3):
                tpl = (p, q, r, s, t)
                if is_clique(tpl, adj):
                    print(tpl, sum(tpl))
                    ans = min((ans, sum(tpl)))
    
    if ans < max_: return ans #答えが確定する場合に返す
Ejemplo n.º 2
0
def main(max_=30000):
    # 各素数に対応する点の次数を管理して、次数が5未満の点は切り捨てたほうが良いかも.
    ans = []
    primes = intlib.primes(max_)
    adj = defaultdict(set)

    for p in primes:
        for q in (q for q in primes if q > p):
            if is_pair(p, q):
                adj[p].add(q)
                adj[q].add(p)

    adj = {p: s for p, s in adj.items() if len(adj[p]) > 4}  #次数4以下の点を削除
    adj = {p: {q for q in ls if q in adj} for p, ls in adj.items()}  #枝を更新
    print(len(adj), sum((len(s) for s in adj.values())) / len(adj))

    keys = sorted(adj.keys())
    ans = float('inf')
    for p in keys:
        if p > ans: break  # pが暫定解を超えたら終了
        adj_p = sorted([x for x in adj[p] if x > p])
        for q in adj_p:
            if len(adj[p] & adj[q]) < 3: continue  #pとqの共通隣接点は3個以上必要
            common_adj = sorted([x for x in adj[p] & adj[q] if x > q])
            for r, s, t in itertools.combinations(common_adj, 3):
                tpl = (p, q, r, s, t)
                if is_clique(tpl, adj):
                    print(tpl, sum(tpl))
                    ans = min((ans, sum(tpl)))

    if ans < max_: return ans  #答えが確定する場合に返す
Ejemplo n.º 3
0
def main():
    TARGET = 50000000
    primes = intlib.primes(int(TARGET ** 0.5))
    p_squares = [p ** 2 for p in primes if p ** 2 <= TARGET]
    p_cubes = [p ** 3 for p in primes if p ** 3 <= TARGET]
    p_fths = [p ** 4 for p in primes if p ** 4 <= TARGET]
    ans = set()
    for a in p_squares:
        for b in p_cubes:
            for c in p_fths:
                if a + b + c <= TARGET:
                    ans.add(a + b + c)

    return len(ans)
Ejemplo n.º 4
0
def pattern(n):
    pre_table = [0] * (n + 1)
    
    for x in range(0, n + 1, 2):
        pre_table[x] = 1
    
    primes = intlib.primes(n)
    
    for k in range(1, len(primes)):
        table = [0] * (n + 1)
        for i in range(n + 1):
            table[i] = sum(pre_table[i - j * primes[k]] for j in
                           range(0, i//primes[k] + 1))
            
        pre_table = table
        
    return pre_table[n]
Ejemplo n.º 5
0
def main():
    _max = 10**4
    odd_iter = (2*n + 1 for n in itertools.count(1))
    primes = intlib.primes(_max)
    primes_set = set(primes) #membership判定用set
    
    for n in odd_iter:
        if n > _max: return 'Not found.'
        if n in primes_set: continue
        flag = True
        for p in primes:
            if p > n: break
            if intlib.is_square((n - p)//2):
                flag = False
                break
        if flag:
            return n
Ejemplo n.º 6
0
'''
The arithmetic sequence, 1487, 4817, 8147, in which each of
the terms increases by 3330, is unusual in two ways:
(i) each of the three terms are prime, and,
(ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or
3-digit primes, exhibiting this property, but there is one other
4-digit increasing sequence.

What 12-digit number do you form by concatenating the three
terms in this sequence?
'''

import intlib
import itertools

if __name__ == '__main__':
    four_digit_primes = [p for p in intlib.primes(10000) if 1000 < p < 10000]
    for a, b, c in itertools.combinations(four_digit_primes, 3):
        if (c-b == b-a and sorted(str(a)) == sorted(str(b)) == sorted(str(c))):
            print(a, b, c)