def main(args): description = ''' Smallest multiple 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? ''' maxp = 999 primes = PrimeNumberPool(maxp) if args.test: logging.info('Running test...') stop = 10 else: stop = 20 result = 1 for p in primes.numbers: if p > stop: break power_of_p = p while (power_of_p <= stop//p): power_of_p *= p result *= power_of_p solution = 'result: {}'.format(result) logging.info(solution)
def main(args): description = ''' Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. ''' if args.test: maxp = 10 else: maxp = 2*1000*1000 ts1 = time.time() primes = PrimeNumberPool(maxp) ts2 = time.time() logging.debug('time for build prime number pool: {}'.format(ts2-ts1)) if primes.GetLargestPrime() > maxp: result = sum(primes.numbers[:-1]) else: result = sum(primes.numbers) solution = 'result: {}'.format(result) logging.info(solution)
def main(args): prime = PrimeNumberPool() for i in range(10000): prime.NewPrime() prime_num4 = [] for p in prime.numbers: if (p > 1000 and p < 10000): prime_num4.append(p) pn4_perm = {} pn4_ps = set() for i in range(len(prime_num4) - 1): p = prime_num4[i] if (p in pn4_ps): continue for j in range(i + 1, len(prime_num4)): q = prime_num4[j] if (q in pn4_ps): continue if (IsPermute(p, q)): pn4_ps.add(p) pn4_ps.add(q) if (p in pn4_perm): pn4_perm[p].append(q) else: pn4_perm[p] = [p, q] for p in list(pn4_perm.keys()): ps = pn4_perm[p] if (len(ps) < 3): continue if (IsArithSeq3(ps)): logging.info(ps) logging.info(IsArithSeq3(ps))
def main(args): prime = PrimeNumberPool() r = 1000 for i in range(r): prime.NewPrime() pb = [] for b in prime.numbers: if (b < r): pb.append(b) pb.append(-b) pb.sort() max_cp = 30 ab = [] for a in range(-r, r): for b in pb: cp = cprime(a,b, prime) if (cp > max_cp): max_cp = cp ab = [a,b] logging.info(ab) logging.info(ab[0]*ab[1])
def main(args): primes = PrimeNumberPool() if args.test: N = 5 for n in [7, 41]: logger.debug("A({}) = {}".format(n, A(n))) else: N = 25 composite = [] t = 1 while len(composite) < N: t += 1 for d in [1, 3, 7, 9]: n = t * 10 + d if primes.IsPrime(n): continue an = A(n) if (n - 1) % an == 0: composite.append(n) logger.debug("A({}) = {}".format(n, an)) logger.debug(composite) answer = sum(composite) logger.info("answer: {}".format(answer))
def main(args): if args.test: M = 50 else: M = 50*1000*1000 prime = PrimeNumberPool() prime.IsPrime(M) rslt = [] for p1 in prime.numbers: n1 = p1*p1 for p2 in prime.numbers: n2 = p2*p2*p2 if (n2 > M): break for p3 in prime.numbers: n3 = p3*p3*p3*p3 if (n3 > M): break n = n1+n2+n3 if (n < M): rslt.append((n, p1, p2, p3)) logging.debug(len(rslt)) ns = set([x[0] for x in rslt]) logging.info("answer: {}".format(len(ns)))
def main(args): description = ''' Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? ''' if args.test: number = 13195 else: number = 600851475143 logging.debug('test with {}'.format(number)) maxp = round(math.sqrt(number)) + 1 ts1 = time.time() primes = PrimeNumberPool(maxp) ts2 = time.time() logging.debug('time for build prime number pool: {}'.format(ts2-ts1)) factors = primes.getPrimeFactor(number) logging.debug('factors:{}'.format(factors)) ts3 = time.time() logging.debug('time for factorize: {}'.format(ts3-ts2)) solution = 'largest prime factor of the number {} is {}'.format(number, factors[-1]) logging.info(solution)
def main(args): if args.test: m = 10 else: m = 1000*1000 prime = PrimeNumberPool() md = 5 mn = 2 for d in range(2, m): if d == 7: continue n = d * 3 // 7 if (n>mn*d/md and HCF(n, d, prime)): mn = n md = d logging.debug("{}/{}".format(mn, md)) logging.info("answer: {}/{}".format(mn, md)) logging.debug(prime.Factorize(mn)) logging.debug(prime.getPrimeFactor(mn)) logging.debug(prime.Factorize(md)) logging.debug(prime.getPrimeFactor(md))
def main(args): t = [1] for i in range(10): t.append(t[-1]*10) prime = PrimeNumberPool() ts1 = time.time() if args.test: max_r = t[9] for i in range(t[4]*1): prime.NewPrime() else: max_r = t[10] for i in range(t[4]*3): prime.NewPrime() ts2 = time.time() logger.debug("prepare primes takes {}s".format(ts2-ts1)) for i in range(len(prime.numbers)): if (i%2==1): continue p2 = prime.numbers[i]*prime.numbers[i] remainder = 2*(i+1)*prime.numbers[i] % p2 if (remainder > max_r): logger.debug((i+1, prime.numbers[i], remainder)) break answer = i+1 logger.info("answer: {}".format(answer))
def main(args): if args.test: K = 20 N = 100 else: K = 1000 N = K*K primes = PrimeNumberPool(K) prime_cube_partnership = [] for k in range(1, K): p = 3*k*(k+1)+1 if primes.IsPrime(p): prime_cube_partnership.append(p) n = k*k*k m = k*k*(k+1) logger.debug("{}^3+{}^2x{} = {}^3".format(n, n, p, m)) if p > N: break logger.debug(prime_cube_partnership) answer = len(prime_cube_partnership) logger.info("answer: {}".format(answer))
def main(args): if args.test: n = 3 else: n = 8 prime = PrimeNumberPool() for i in range(2,n): logging.debug(ispanprime(list(range(1,i+1)), prime))
def main(args): if args.test: m = 100 * 1000 maxprime = 1000 else: m = 10 * 1000 * 1000 maxprime = 100000 t1 = time.time() prime = PrimeNumberPool(maxprime) t2 = time.time() logging.debug("time for build prime pool:{}".format(t2 - t1)) ''' Analysis By observation, the numbers with high φ(n) are product of two prime numbers ''' max_ratio = (2, 1) num_prime = len(prime.numbers) for i in range(num_prime): p1 = prime.numbers[i] if p1 * p1 > m: break for j in range(i, num_prime): p2 = prime.numbers[j] if p1 * p2 > m: break n = p1 * p2 phi = n - p1 - p2 + 1 if (IsPermute(n, phi)): if (max_ratio[0] * phi > max_ratio[1] * n): logging.debug("{} factor to {}".format( n, prime.Factorize(n))) max_ratio = (n, phi) logging.debug(max_ratio) ''' # brute force way for n in range(3,m,2): # pre-qualify pre_qualify = 1 for p in prime.numbers: if (n % p == 0 and p*max_ratio[1] > (p-1)*max_ratio[0]): pre_qualify = 0 break if (p * p > n): break if (pre_qualify): pn = Phi(n,prime) if (IsPermute(n, pn)): if (max_ratio[0]*pn > max_ratio[1]*n): logging.debug("{} factor to {}".format(n, prime.Factorize(n))) max_ratio = (n, pn) logging.debug(max_ratio) ''' logging.info("answer: {}".format(max_ratio[0]))
def main(args): if args.test: n = 100 else: n = 647 prime = PrimeNumberPool() while (len(defactor(n, prime)) < 4 or len(defactor(n + 1, prime)) < 4 or len(defactor(n + 2, prime)) < 4 or len(defactor(n + 3, prime)) < 4): n += 1 logging.info(n)
def main(args): prime = PrimeNumberPool() n = 33 done = 0 while (done == 0): if (prime.IsPrime(n)): n += 2 continue if (Check(n, prime) == 0): logging.info(n) done = 1 else: n += 2
def main(args): primes = PrimeNumberPool() num_d = 5 num_p = 3 n = 3 while (float(num_p) / num_d > 0.1): n += 2 p = n * n - 3 * n + 3 for i in range(4): if (primes.IsPrime(p)): num_p += 1 p += n - 1 num_d += 4 logging.info(n)
def main(args): if args.test: m = 3000 max_phi = 1 max_n = 1 brute_force = 1 else: m = 1000 * 1000 max_phi = 3 max_n = 6 brute_force = 0 prime = PrimeNumberPool(m) ''' A strait forward way is to calculate the product of primes ''' answer = 1 for p in prime.numbers: while answer < m: answer *= p logging.info("answer:{}".format(answer)) return 0 max_num_prime_factors = 1 for n in range(2, m + 1): if brute_force: phi = Phi(n, prime) else: ''' Analysis: A lower phi happens when the number n has many prime factors ''' factors = prime.getPrimeFactor(n) if len(factors) > max_num_prime_factors: max_num_prime_factors = len(factors) phi = Phi(n, prime) logging.debug("{} factors: {}, phi: {}".format( n, factors, phi)) else: continue logging.debug("{} phi = {}".format(n, phi)) if (phi * max_phi < n): max_n = n max_phi = float(n) / phi logging.debug((max_n, max_phi)) logging.info("answer:{}".format(max_n))
def main(args): if args.test: m = 10 else: m = 1000 * 1000 ts1 = time.time() prime = PrimeNumberPool(m // 2) ts2 = time.time() logging.debug("prepare primes takes {} seconds".format(ts2 - ts1)) s = 0 for d in range(2, m + 1): nrf = Phi(d, prime) s += nrf logging.info("answer: {}".format(s))
def main(args): prime = PrimeNumberPool() for i in range(80000): prime.NewPrime() all_prime = set(prime.numbers) truncatable_prime = [] for p in all_prime: if (p < 11): continue if (is_tr_prime(p, all_prime)): truncatable_prime.append(p) logging.debug(truncatable_prime) logging.info(sum(truncatable_prime))
def main(args): if args.test: mp = 1000 else: mp = 1000000 prime = PrimeNumberPool(mp) bucket = {} for p in prime.numbers: Bucketize(p, bucket) for s in list(bucket.keys()): if (len(bucket[s]) == 8): logging.debug((s, bucket[s])) logging.info("solution: {}".format(bucket[s][0]))
def main(args): primes = PrimeNumberPool() odds = [1, 3, 5, 7, 9] cir_prime = [2, 3, 5, 7] for d1 in odds: for d2 in odds: if (check([d1, d2], primes)): cir_prime.append(d1 * 10 + d2) for d1 in odds: for d2 in odds: for d3 in odds: if (check([d1, d2, d3], primes)): cir_prime.append(d1 * 100 + d2 * 10 + d3) for d1 in odds: for d2 in odds: for d3 in odds: for d4 in odds: if (check([d1, d2, d3, d4], primes)): cir_prime.append(d1 * 1000 + d2 * 100 + d3 * 10 + d4) for d1 in odds: for d2 in odds: for d3 in odds: for d4 in odds: for d5 in odds: if (check([d1, d2, d3, d4, d5], primes)): cir_prime.append(d1 * 10000 + d2 * 1000 + d3 * 100 + d4 * 10 + d5) for d1 in odds: for d2 in odds: for d3 in odds: for d4 in odds: for d5 in odds: for d6 in odds: if (check([d1, d2, d3, d4, d5, d6], primes)): cir_prime.append(d1 * 100000 + d2 * 10000 + d3 * 1000 + d4 * 100 + d5 * 10 + d6) logging.debug(cir_prime) logging.info(len(cir_prime))
def main(args): if args.test: m = 8 + 1 else: m = 1000 * 12 + 1 prime = PrimeNumberPool(m // 2) num = 0 for d in range(3, m): d1 = d // 2 + 1 pd = prime.getPrimeFactor(d) for n in range(d // 3 + 1, d1): if (HCF(n, pd)): logging.debug("{}/{}".format(n, d)) num += 1 logging.info("answer: {}".format(num))
def main(args): description = ''' Highly divisible triangular number Problem 12 The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: 1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? ''' ''' Analysis: the n-th triangle number is n*(n+1)/2 ''' primes = PrimeNumberPool() if args.test: break_cond = 5 else: break_cond = 500 n = 1 while (True): n += 1 tri = n * (n + 1) // 2 ndiv = num_divisors(tri, primes) if ndiv > break_cond: solution = 'result: {}'.format(tri) logging.info(solution) break
def main(args): prime = PrimeNumberPool() cprime = set() ts1 = time.time() m = 2000 for i in range(m): prime.NewPrime() pm = prime.numbers[:m] ts2 = time.time() logging.debug("time for build primes {}".format(ts2 - ts1)) for i in range(m): p1 = pm[i] for j in range(i, m): p2 = pm[j] if (not prime.IsPrime(CatNum(p1, p2))): continue if (not prime.IsPrime(CatNum(p2, p1))): continue cprime.add((p1, p2)) logging.debug(len(cprime)) ts3 = time.time() logging.debug("time for build prime pairs {}".format(ts3 - ts2)) for (p1, p2) in cprime: for p3 in pm: if (p3 <= p2): continue if (not Check(p3, [p1, p2], cprime)): continue for p4 in pm: if (p4 <= p3): continue if (not Check(p4, [p1, p2, p3], cprime)): continue for p5 in pm: if (p5 <= p4): continue if (not Check(p5, [p1, p2, p3, p4], cprime)): continue logging.info((p1, p2, p3, p4, p5)) logging.info("result: {}".format(sum([p1, p2, p3, p4, p5]))) return 0
def main(args): prime = PrimeNumberPool() n = 100000 rod = {} for k in range(1, n+1): f = prime.getPrimeFactor(k) p = np.product(f) if (p in rod): rod_p = rod[p] rod_p.append(k) else: rod[p] = [k] # assume rod's key is sorted e_n = [] for k in sorted(list(rod.keys())): e_n += rod[k] logger.info("answer: {}".format(e_n[10000-1]))
def main(args): description = ''' Largest palindrome product A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. ''' # analysis: # product of two 3-digit numbers <= 999*999 = 998001 # start from the lagest palindrome, and try to factorize to product of two 3-digit numbers # maxp = 999 primes = PrimeNumberPool(maxp) if args.test: logging.info('Running test...') # construct palindromic number for a in range(9, 0, -1): p1 = 1001*a for b in range(9, -1, -1): p2 = p1 + 110*b factor2 = Factorize2(p2, primes) if len(factor2) > 0: solution = 'largest palindrome {} made from the product of {}'.format(p2, factor2) logging.info(solution) exit(0) else: t1 = time.time() # construct palindromic number for left in range(999, 100, -1): p = MirrorNumber(left) factor3 = Factorize3(p, primes) if len(factor3) > 0: solution = 'largest palindrome {} made from the product of {}'.format(p, factor3) logging.info(solution) t2 = time.time() logging.info('run time {}'.format(t2-t1)) exit(0)
def main(args): ml = 1 mp = 2 if args.test: m = 1000 else: m = 1000000 prime = PrimeNumberPool(m) logging.debug(len(prime.numbers)) for i in range(len(prime.numbers)): p = prime.numbers[-i-1] cp = ConsecutivePrime(p, prime, ml) if (len(cp) > ml): ml = len(cp) mp = p logging.info((mp, ml))
def main(args): description = ''' 10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? ''' primes = PrimeNumberPool() if args.test: logging.info('Running test...') idx = 6 - 1 else: idx = 10001 - 1 num_primes = primes.NumberOfPrimes() for i in range(idx + 1 - num_primes): primes.NewPrime() result = primes.numbers[idx] solution = 'result: {}'.format(result) logging.info(solution)
5 3 1 5557 6 3 1 6661 7 3 9 57863 8 3 1 8887 9 3 7 48073 For d = 0 to 9, the sum of all S(4, d) is 273700. Find the sum of all S(10, d). """ import time from prime import PrimeNumberPool import logging logger = logging.getLogger('p111') prime = PrimeNumberPool(120000) def M1(n, d): N = [] dd = d * (10**n - 1) // 9 n0 = 10**(n - 1) for a in range(10): for i in range(n): k = dd + (a - d) * (10**i) if k < n0: continue if prime.IsPrime(k): N.append(k) return N
# Investigating a Prime Pattern # Problem 146 # # The smallest positive integer n for which the numbers # n^2+1, n^2+3, n^2+7, n^2+9, n^2+13, and n^2+27 are consecutive primes is 10. # The sum of all such integers n below one-million is 1242490. # What is the sum of all such integers n below 150 million? from prime import PrimeNumberPool primes = PrimeNumberPool(load_from="data/prime_numbers.txt") import logging import time logger = logging.getLogger('p146') # Analysis # let p = n^2+1 # p, p_2, p+6, p+8, p+12, p+26 are all primes, # then p = 11 or p = 101 mod 210 # thus n = 210k + (10, 80, 130, 200) # Let n = qx + r where q is prime. # Then n^2 + s = q^2 x + 2qrx + (r^2 + s). # If q | (r^2 + s), then q | (n^2 + s). # So we do not want those n that satisfy q | ((n%q)^2 + s) # for some prime q and some s in {1, 3, 7, 9, 13, 27}. def filter_on_prime(n, ps, cs): for p in ps: r = n % p
# Prime pair connection # Problem 134 # Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that 1219 is the smallest number such that the last digits are formed by p1 whilst also being divisible by p2. # In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive primes, p2 > p1, there exist values of n for which the last digits are formed by p1 and n is divisible by p2. Let S be the smallest of these values of n. # Find ∑ S for every pair of consecutive primes with 5 ≤ p1 ≤ 1000000. import pickle import time from prime import PrimeNumberPool from util import ExtendedEuclidean import logging logger = logging.getLogger('p134') primes = PrimeNumberPool() def S(p1, p2): t = 10 while t < p1: t *= 10 # find out n*t+p1 = 0 (mod p2) # n*t = p2-p1 mod p2 # n*t = k*p2 = (p2-p1) # since GCD(t, p2) = 1, # we can find x*t+y*p2 = 1 # then n = x*(p2-p1), k = y*(p2=p1) x, y = ExtendedEuclidean(t, p2) n = (x * (p2 - p1) % p2) * t + p1 return n