def maxconsecutiveprime(x): ps = primes(x) s = 0 for i, p in enumerate(ps): s += p if s > x: break ps2 = ps[:i] l = len(ps2) for i in range(l)[::-1]: for j in range(l - i)[::-1]: pp = ps2[j:j + i + 1] if sum(pp) in ps: return sum(pp), len(pp), pp
#! /usr/bin/env python3 ''' Problem 46 - Project Euler http://projecteuler.net/index.php?section=problems&id=046 ''' import math from mathtools import primes if __name__ == '__main__': N = 1_000_0 ps = primes(N) odd_composite_num = list(set(range(5, N+1, 2)) - set(ps)) for i in odd_composite_num: for sq in range(1, int(math.sqrt(i/2))+1)[::-1]: if (i - 2 * (sq ** 2)) in ps: break else: print(i) break
def main(args): return mt.item_by_index(mt.primes(), args.prime_index)
#! /usr/bin/env python3 ''' Problem 26 - Project Euler http://projecteuler.net/index.php?section=problems&id=026 ''' from mathtools import primes def countcycle(x): count, n = 0, 1 while n % x == n: count += 1 n = n * 10 while n % x != 1: if n % x == 0: return 0 count += 1 n = (n % x) * 10 return count if __name__ == '__main__': n = 1000 ps = primes(n) ps.remove(2) ps.remove(5) cclist = map(lambda x: (x, countcycle(x)), ps) print(max(cclist, key=(lambda x: x[1])))
#! /usr/bin/env python3 ''' Problem 7 - Project Euler http://projecteuler.net/index.php?section=problems&id=7 ''' from mathtools import primes NUM = 10_001 if __name__ == '__main__': pmax = 110000 ps = primes(pmax) while len(ps) < NUM: pmax += 10000 ps = primes(pmax) else: print(str(ps[NUM - 1]) + ' is the {}st prime number.'.format(NUM))
#! /usr/bin/env python3 ''' Problem 49 - Project Euler http://projecteuler.net/index.php?section=problems&id=049 ''' from mathtools import primes from itertools import combinations from functools import reduce from operator import add def samedigits(*xs): if len(xs) < 2: return True x = set(str(xs[0])) return all([set(str(y)) == x for y in xs[1:]]) if __name__ == '__main__': ps = list(filter(lambda x: x > 999, primes(9999))) cs = [c for c in combinations(ps,2) if samedigits(*c) and (2 * max(c) - min(c)) in ps] ans = [] for c in cs: x, y = min(c), max(c) z = 2 * y - x if samedigits(x, y, z): ans.append((x, y, z)) print(ans) print(list(map(lambda x: reduce(add, map(str, x)), ans)))