def maxprime(n): primes = list(primes_until(n)) maxp = maxlen = 2 for start in xrange(len(primes)): for end in xrange(start + maxlen + 1, start + 800): if end - start < maxlen: break conseq = primes[start:end] s = sum(conseq) if s >= 1000000: break if s in primes[start + maxlen:]: maxlen = end - start maxp = s return maxp
#!/usr/bin/env python from utils import primes_until MAX = 10000 comps = [False] * MAX comps[1] = True for p in primes_until(MAX - 1): q = 1 comps[p] = True while True: pplusq = p + 2 * q**2 if pplusq >= MAX: break comps[pplusq] = True q += 1 for i, passed in enumerate(comps): if i & 1 and not passed: print i break
#! python3 """Find the sum of all the primes below two million.""" import sys from os.path import dirname sys.path.insert(0, dirname(dirname(__file__))) from utils import primes_until print(sum(primes_until(2 * 10**6)))
#!/usr/bin/env python from itertools import permutations, combinations from utils import primes_until, add from sys import exit def concat(digits): return int(reduce(add, digits)) def suited(n): return n > 999 and n not in (1487, 4817, 8147) and n in primes primes = [p for p in primes_until(10000) if p > 999] for p in primes: perm = map(concat, permutations(str(p))) for a, b, c in set(combinations(perm, 3)): if a >= b or a >= c or b >= c \ or b - a != c - b or b - a < 1000 \ or not all(map(suited, (a, b, c))): continue print '%d%d%d' % (a, b, c) exit()
#!/usr/bin/env python from utils import primes_until MAX = 10000 comps = [False] * MAX comps[1] = True for p in primes_until(MAX - 1): q = 1 comps[p] = True while True: pplusq = p + 2 * q ** 2 if pplusq >= MAX: break comps[pplusq] = True q += 1 for i, passed in enumerate(comps): if i & 1 and not passed: print i break
#! python3 """How many circular primes are there below one million?""" from time import time import sys from os.path import dirname sys.path.insert(0, dirname(dirname(__file__))) from utils import primes_until def is_circular(n): if n in primes: s = str(n) for i in range(1, len(s)): if int(s[i:] + s[:i]) not in primes: break else: return True t0 = time() primes = set(primes_until(10**6)) # Try using list or tuple to see the diff solution = sum(1 for i in range(2, 10**6) if is_circular(i)) print("%d in %.2f seconds" % (solution, time() - t0))
# The incredible formula n^2 - 79n + 1601 was discovered, which produced 80 # primes for the consecutive values 0 <= n <= 79. The product of the # coefficients, -79 and 1601, is -126479. # # Considering quadratics of the form: # # n^2+an+b, where |a|<1000 and |b| <= 1000 # # where |n| is the modulus/absolute value of n # e.g. |11| = 11 and |-4| = 4. # Find the product of the coefficients, a and b, for the quadratic expression # that produces the maximum number of primes for consecutive values of n, # starting with n=0. import sys sys.path.insert(0, '../common/') import utils p=set(utils.primes_until(100000)) x=[(a,b) for a in range(-999,1000) for b in range(-1000,1001)] len(x) n=0 while True: f={(a,b):n*n + a * n + b for (a,b) in x if abs(n*n + a * n + b) in p} if not f: break x=f.keys() n=n+1 (a,b)=x[0] print a*b
#!/usr/bin/env python from __future__ import division from utils import primes_until primes = list(primes_until(10000)) def distinct(n, frm=0): for i, p in enumerate(primes[frm:]): div = n / p if div < 2: break if div.is_integer(): others = set([int(div)]) if div in primes[i:] else distinct(div, i) if others: return others | set([p]) n = N = 4 counter = 0 while counter != N: factors = distinct(n) counter = counter + 1 if factors and len(factors) == N else 0 n += 1 print n - N
# 1/8 = 0.125 # 1/9 = 0.(1) # 1/10 = 0.1 # Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. # It can be seen that 1/7 has a 6-digit recurring cycle. # # Find the value of d < 1000 for which 1/d contains # the longest recurring cycle in its decimal fraction part. # Solution: # # Find the number < 1000 with the highest discrete log for 10. # This will be a prime p with discrete-log_10(1) mod p = p - 1. # We factor p - 1. Fermats little theorem gives us 10^(p-1) = 1 mod (p) # Check that 10^x != 1 mod(p) for all proper factors of p-1. Then p - 1 # is the discrete log and thus the cycle length. # # A prime p with max cycle at p-1, will have only one exponent, p - 1 itself, # for which 10^x == 1 (mod p). So: # Find all factors of p - 1 # Do modexp(10, x, p) for each factor. # Count the number of results = 1. # Filter for count = 1 # Take the max of the primes that pass the filter. import sys sys.path.insert(0, '../common/') import utils print max(filter(lambda p: map(lambda x: utils.modexp(10, x, p), utils.factors(p-1)).count(1)==1, utils.primes_until(1000)))