def p087(): LIM = 50000000 p4s = list(primes_less_than(int(math.pow(LIM, 1.0 / 4.0) + 1))) p3s = list(primes_less_than(int(math.pow(LIM, 1.0 / 3.0) + 1))) p2s = list(primes_less_than(int(math.pow(LIM, 1.0 / 2.0) + 1))) results = set() for p4 in p4s: for p3 in p3s: for p2 in p2s: results.add(p4 ** 4 + p3 ** 3 + p2 ** 2) # print (p2,p3,p4,p4**4+p3**3+p2**2) return sum(1 for r in results if r < LIM)
def using_sieve(): ps = list(primes_less_than(LIMIT+1)) totients = [n for n in xrange(0, LIMIT+1)] totients[0] = totients[1] = 0 for p in ps: for n in xrange(p, LIMIT+1, p): totients[n] = totients[n] * (p-1) / p return sum(totients)
def p077(): denoms = list(reversed(list(primes_less_than(100)))) for i in xrange(10, 100): count = combos(i, 0, denoms) if i in denoms: count -= 1 if count > 5000: return i
def p046(): LIMIT = 100000 good = set(range(9,LIMIT,2)) squares = [2*n*n for n in xrange(1, int(math.sqrt(LIMIT//2))+1)] for p in primes_less_than(LIMIT): for s in squares: good.discard(p) good.discard(p+s) return list(good)[0]
def fast(count, limit): divider = reduce(operator.mul, list(primes_less_than(100))[:count-1], 1) factors = dict() for p in primes_less_than(limit//divider): for n in xrange(p, limit, p): factors[n] = factors.setdefault(n, 0) + 1 candidates = set(k for k,v in factors.iteritems() if v==count) for c in sorted(list(candidates)): found = True for d in xrange(c+1, c+count): if d not in candidates: found = False break if found: return c return 0
def p049(): four_digit_primes = [p for p in primes_less_than(10000) if p > 999] perms = dict() for f in four_digit_primes: perms.setdefault(''.join(sorted(str(f))), []).append(f) candidates = [v for k, v in perms.iteritems() if len(v) > 2] for c in candidates: for ariths in find_ariths(c): if ariths[0] != 1487: return int("%d%d%d" % (ariths[0], ariths[1], ariths[2]))
def p050(): limit = 1000000 ps = list(primes_less_than(limit)) l_l = 0 l_p = 0 l = len(ps) for i in xrange(0, l-l_l+1): sum = 0 plus = 0 while sum < limit and i+plus < l-l_l+1: sum += ps[i+plus] plus += 1 if sum < limit and is_prime(sum) and plus > l_l: l_l = plus l_p = sum return l_p
def circular_primes(limit): result = [] primes = primes_less_than(limit) primes_dict = defaultdict(bool) for p in primes: primes_dict[p] = True count = 0 for p in primes: if p in result: continue rotations = get_rotations(p) if all([primes_dict[x] == True for x in rotations]): for r in rotations: result.append(r) return sorted(result)
def p070(): winning_n = 87109 winning_ratio = 87109.0 / 79180 LIMIT = 10 ** 7 ps = list((p for p in primes_less_than(LIMIT / 11) if p >= 11)) pl = len(ps) for i in xrange(pl - 1, 0, -1): x = ps[i] j, y, n = 0, 11, 11 * x while n < LIMIT: t = (x - 1) * (y - 1) ratio = n * 1.0 / t if ratio < winning_ratio: if perms(n, t): winning_n = n winning_ratio = ratio j += 1 y = ps[j] n = x * y return winning_n
def tup1_gen(): for p1 in primes(): for p2 in primes_less_than(p1): if p1 != 2 and p1 != 5 and p2 != 2 and p2 != 5: if co_concat_prime(p2, p1): yield (p2,p1)
from primes import primes_less_than import math primes = primes_less_than(10000) def can(odd_number): for p in primes: if p > odd_number: break diff = odd_number - p if diff % 2 != 0: continue square = diff / 2 x = int(math.sqrt(square)) if x*x == square: return True return False for i in range(35, 99999, 2): if not can(i): print i break
def p037(): truncs = [n for n in primes_less_than(1000000) if truncatable(n)] return sum(truncs)
def primes_from_to(start, stop): for p in primes.primes_less_than(stop): if p >= start: yield p
import primes def primes_from_to(start, stop): for p in primes.primes_less_than(stop): if p >= start: yield p list(primes.primes_less_than(1000000)) known_primes = set(primes.primes.known_primes) maxp = 0 maxsl = 0 for a in range(len(primes.primes.known_primes)): for b in range(a-1-maxsl): s = primes.primes.known_primes[b:a] if len(s) > maxsl: if sum(s) in known_primes: print sum(s), s maxsl = len(s) maxp = sum(s)
from primes import primes_less_than primes = set(primes_less_than(1000000)) print 'starting ..' def iss(p): count = 0 #seen = set() li = list(str(p)) for x in range(10): for i in range(len(li)): for j in range(len(li)): ii = li[i] jj = li[j] li[i] = li[j] = str(x) #print int(''.join(li)) y = int(''.join(li)) #print y #if x in seen: continue if y in primes: #seen.add(x) #print li count += 1 li[i] = ii li[j] = jj if count == 8: return True return False for p in primes: if iss(p): print p
def get_4d_primes(): for p in primes.primes_less_than(10000): if p > 1000: yield p
def all_co_good(p1, p2): if (p1,p2) in acg_known: return acg_known[(p1,p2)] val = [p for p in primes_less_than(p2) if p>p1 and co_concat_prime(p1,p) and co_concat_prime(p,p2)] acg_known[(p1,p2)] = val return val
import primes primes.primes_less_than(100000000)
def using_generation(): ps = list(primes_less_than(LIMIT+1)) + [LIMIT+1, LIMIT+2] return sums(1, 1, 0, ps)