def S(n): primes = sieve(n) print 'primes done' total = 0 for i in range(len(primes)): if primes[i] * primes[i + 1] > n: break print i p = primes[i] for j in range(i + 1, len(primes)): q = primes[j] if p * q > n: break total += M(p, q, n) return total
def euler_phi(n): primes = sieve(1000) answer = 1 for prime in primes: if n == 1: break power = 0 while n % prime == 0: power += 1 n /= prime if power > 0: answer *= (prime - 1) * prime**(power - 1) assert n == 1 or isPrimeMR(n) if n > 1: answer *= n - 1 return answer
def lcm(n): primes = sieve(n + 1) l = range(1, n + 1) r = 1 for i in range(n): while r % l[i] != 0: for p in primes: if l[i] % p == 0: r *= p if r % l[i] == 0: continue j = 0 while j < len(primes): p = primes[j] if l[i] % p == 0: l[i] /= p r *= p else: j += 1 return r
def lcm(n): primes = sieve(n+1) l = range(1,n+1) r = 1 for i in range(n): while r % l[i] != 0: for p in primes: if l[i] % p == 0: r *= p if r % l[i] == 0: continue j = 0 while j < len(primes): p = primes[j] if l[i] % p == 0: l[i] /= p r *= p else: j += 1 return r
from helpers import prime_factorizations, crt, sieve from itertools import product ''' 153651073760956 [Finished in 2605.0s] ''' limit = 2 * 10**7 # N = 2 * 10 ** 7 pfs = prime_factorizations(limit / 20) primes = sieve(limit) setprimes = set(primes) def get_pf(n): if n < len(pfs): return pfs[n] else: if n in setprimes: return {n : 1} ans = {} for p in primes: if n < len(pfs) or n in setprimes: break power = 0 while n % p == 0: power += 1 n /= p
# A sieve could be better at this, but requires a good estimate on the upper bound of the nth prime which I didn't know off hand primes = [2,3,5,7,11,13,17,19,23,29] sprimes = [2,3,5] # primes up to the square root of the biggest prime in primes n = primes[-1] while len(primes) < 10001: n += 2 if n ** 0.5 > sprimes[-1]: sprimes.append(primes[len(sprimes)]) if all([n % p != 0 for p in sprimes]): primes.append(n) print primes[-1] # I later found that nlogn + nloglogn is a good upperbound approximation: from math import log from helpers import sieve n = 10001 print sieve(int(n*log(n) + n*log(log(n))))[10000]
from fractions import Fraction from helpers import sieve primes = set(sieve(500)) memo = {} def left_hop(n): if n > 1: return n - 1 assert n == 1 return 2 def right_hop(n): if n < 500: return n + 1 assert n == 500 return 499 def probability(n, string): if (n,string) in memo: return memo[(n, string)] elif len(string) == 1: if n in primes and string == 'P': memo[(n,string)] = Fraction(2, 3) return Fraction(2, 3) if n in primes and string == 'N': memo[(n,string)] = Fraction(1, 3) return Fraction(1, 3)
from helpers import sieve from itertools import combinations from fractions import Fraction primes = sieve(25) n = reduce(lambda a, b: a * b, primes) phin = reduce(lambda a, b: a * b, [i - 1 for i in primes]) ''' Just have to get i as small as possible where the first printed value is less than the second. Answer is the third. ''' i = 4 print float(i * phin) / (i * n - 1) print float(15499) / 94744 print i * n for (p1, p2) in combinations(primes, 2): potential_n = n * p1 phi_potential_n = p2 * phin * p1 / (p1 - 1) if float(phi_potential_n) / (potential_n - 1) < float(15499) / 94744: print 'wtf'
#Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. from helpers import sieve p = sieve(1000)[3:] def cyclelen(n): k = 1 while True: if 10**k % n == 1: return (k, n) k += 1 print max(map(cyclelen, p))
from helpers import sieve, prime_factorizations, isPrimeMR primes = sieve(10**8) setprimes = set(primes) pfs = prime_factorizations(10**6) def get_divisors(pf): if len(pf) == 0: yield 1 return (prime, power) = pf.popitem() for p in range(power + 1): for divisor in get_divisors(pf): yield prime**p * divisor pf[prime] = power def get_pf(n): if n < len(pfs): return pfs[n] else: if isPrimeMR(n): return {n : 1} ans = {} for p in primes: if n < len(pfs) or n in setprimes: break power = 0 while n % p == 0:
from helpers import get_prime_mask, sieve from itertools import permutations limit = 1000000 primes = sieve(limit) is_prime = get_prime_mask(primes, limit) def rotate_string(s): for i in range(len(s)): s = s[1:] + s[0] yield s cycle_primes = [] for p in primes: all_prime = True for perm in rotate_string(str(p)): i = int(''.join(perm)) all_prime = all_prime & is_prime[i] if all_prime: cycle_primes.append(p) print len(cycle_primes)
# This is really dumb and takes forever to run, I know # A smarter way would just check random combos matching ([3,7,9][1,3,7,9]*[3,7,9]) until you find 11 from helpers import sieve, is_prime print sum([ int(p) for p in map(str, sieve(1000000)[4:]) if all([ is_prime(int(p[i:])) and is_prime(int(p[:i + 1])) for i in range(len(p)) ]) ])
from helpers import sieve print sum(sieve(2000000))
from helpers import sieve, rotations primes = set(map(str, sieve(1000000))) print len([p for p in primes if all([r in primes for r in rotations(p)])])
from helpers import sieve, is_prime p = sieve(10000) total = 0 start = 0 length = 0 maxlen = 0 while True: if total + p[start+length] < 1000000: total += p[start+length] length += 1 if length > maxlen and is_prime(total): if 1000000 - total < p[start+length]: print total break maxlen = length else: total -= (p[start] + p[start+length-1]) length -= 2 start += 1
import copy from helpers import sieve primes = sieve(30000) #there's always a solution given for k by 2k = (k,2,1,1,1,1,...,1) so we only need to look in range(1,k+1) table_of_vals = [30000 for i in range(12001)] table_of_vals[0] = 0 table_of_vals[1] = 0 sum_list = [[] for i in range(24001)] def multiply_set(n): if(n in primes): return([[n]]) l = [] for j in range(2,int(n**0.5)+1): if(n % j == 0): if(sum_list[int(n/j)] != []): a = copy.deepcopy(sum_list[int(n/j)]) else: a = copy.deepcopy(multiply_set(int(n/j))) for i in a: i.append(j) i.sort() if(l.count(i) == 0): l.append(i) l.append([n]) sum_list[n] = l return(l) def multiply_and_sum_set(n): x = copy.deepcopy(multiply_set(n))
def int_split(n, d): left = n / 10**d right = n % 10**d if int_concat(left, right) == n: return (left, right) def is_family(f): for i in range(len(f)): for j in range(i + 1, len(f)): left = int_concat(f[i], f[j]) right = int_concat(f[j], f[i]) if not (is_prime(left) and is_prime(right)): return False return True sieve(100000000) max_prime = 10000 fam_size = 3 primes = list(prime_gen(max_size = max_prime)) def fams(): for i in range(1, len(primes)): for j in range(i+1, len(primes)): if not is_family((primes[i], primes[j])): continue print (primes[i], primes[j]) for k in range(j+1, len(primes)): if not is_family((primes[i], primes[j], primes[k])): continue for l in range(k + 1, len(primes)): if not is_family((primes[i], primes[j], primes[k], primes[l])) or k > l:
""" 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? """ from helpers import sieve, primeUpperBound print sieve(primeUpperBound(10001))[10000]
Observation: for a given fraction, the number of steps before reducing is the number of steps before we hit the next smallest factor. We always reduce to 1 / another number and so we always eliminate all of the smallest factors. So f(n) = largest_prime_factor(n + 1) - 1. ''' from helpers import prime_factorizations, isPrimeMR, sieve limit = 2 * 10**6 primes = sieve(limit + 100) pfs = prime_factorizations(limit + 100) def get_pf(n): if n < len(pfs): return pfs[n] else: if isPrimeMR(n): return {n: 1} ans = {} for p in primes: if n < len(pfs) or isPrimeMR(n): break power = 0
from helpers import sieve, prime_factorizations, isPrimeMR primes = sieve(10**8) setprimes = set(primes) pfs = prime_factorizations(10**6) def get_divisors(pf): if len(pf) == 0: yield 1 return (prime, power) = pf.popitem() for p in range(power + 1): for divisor in get_divisors(pf): yield prime**p * divisor pf[prime] = power def get_pf(n): if n < len(pfs): return pfs[n] else: if isPrimeMR(n): return {n: 1} ans = {} for p in primes: if n < len(pfs) or n in setprimes: break power = 0 while n % p == 0:
import copy from helpers import sieve primes = sieve(30000) #there's always a solution given for k by 2k = (k,2,1,1,1,1,...,1) so we only need to look in range(1,k+1) table_of_vals = [30000 for i in range(12001)] table_of_vals[0] = 0 table_of_vals[1] = 0 sum_list = [[] for i in range(24001)] def multiply_set(n): if (n in primes): return ([[n]]) l = [] for j in range(2, int(n**0.5) + 1): if (n % j == 0): if (sum_list[int(n / j)] != []): a = copy.deepcopy(sum_list[int(n / j)]) else: a = copy.deepcopy(multiply_set(int(n / j))) for i in a: i.append(j) i.sort() if (l.count(i) == 0): l.append(i) l.append([n]) sum_list[n] = l return (l)
n = [i for i in str(n)] m = [i for i in str(m)] while len(n) > len(m): answer += num_transitions[int(n[0])] n = n[1:] for i in range(len(n)): num1 = int(n[i]) num2 = int(m[i]) answer += num_transition_paired[(num1, num2)] return answer def max_transitions_required(n): answer = 0 answer += sum([num_transitions[int(i)] for i in str(n)]) while digit_sum(n) != n: answer += smart_transitions_required(n, digit_sum(n)) n = digit_sum(n) answer += sum([num_transitions[int(i)] for i in str(n)]) return answer primes = [p for p in sieve(2 * 10**7) if p > 10**7] print 'primes found' answer = 0 for p in primes: answer += sam_transitions_required(p) - max_transitions_required(p) print answer # print sam_transitions_required(137) # print max_transitions_required(137)
if i < p: return 0 a = num_fac_powers(p, i / p) if len(memo) > 100000: memo.clear() memo[(p, i)] = i/p + a return i/p + a def get_smallest_possible(p, i): min_possible_power = c * num_fac_powers(p, i) potential_n = p + p * (((p - 1) * min_possible_power) / p) while num_fac_powers(p, potential_n) < min_possible_power: potential_n += p return potential_n primes = sieve(N) setprimes = set(primes) print len(primes) last_chosen_p = 1 last_chosen_p = 1 answer = 0 for i in range(N, 9, -1): if i % 100 == 0: print i if (i+1)/last_chosen_p == i/last_chosen_p and i < N: answer += get_smallest_possible(last_chosen_p, i) continue
from helpers import sieve #from itertools import takewhile #from helpers import get_primes print sum(sieve(2000000)) #total = 0 #for p in get_primes(): # if p < 2E6: # total += p # else: # break #print total
from helpers import sieve, is_prime p = sieve(10000) total = 0 start = 0 length = 0 maxlen = 0 while True: if total + p[start + length] < 1000000: total += p[start + length] length += 1 if length > maxlen and is_prime(total): if 1000000 - total < p[start + length]: print total break maxlen = length else: total -= (p[start] + p[start + length - 1]) length -= 2 start += 1
Observation: for a given fraction, the number of steps before reducing is the number of steps before we hit the next smallest factor. We always reduce to 1 / another number and so we always eliminate all of the smallest factors. So f(n) = largest_prime_factor(n + 1) - 1. ''' from helpers import prime_factorizations, isPrimeMR, sieve limit = 2 * 10**6 primes = sieve(limit + 100) pfs = prime_factorizations(limit + 100) def get_pf(n): if n < len(pfs): return pfs[n] else: if isPrimeMR(n): return {n : 1} ans = {} for p in primes: if n < len(pfs) or isPrimeMR(n): break power = 0 while n % p == 0:
from helpers import sieve lim = 50000000 primes = sieve(int(lim**0.5)+1) l = [] for p1 in primes: for p2 in primes: if(p1**2+p2**3 > lim): break for p3 in primes: if(p1**2+p2**3+p3**4 > lim): break l.append(p1**2+p2**3+p3**4) l = list(set(l)) print(len(l))
# I could probably improve this to not recalculate n(v) over and over but instead build it all at once # But I had basically already written n(v) for solving the earlier coin problem, and it was easy to reuse from helpers import sieve primes = sieve(100) def n(v): ways = [1] + [0] * v for prime in primes: for i in range(prime, v + 1): ways[i] += ways[i - prime] return ways[v] v = 2 while n(v) < 5000: v += 1 print v, n(v)
from helpers import sieve from itertools import combinations from fractions import Fraction primes = sieve(25) n = reduce(lambda a, b : a * b, primes) phin = reduce(lambda a,b : a * b, [i - 1 for i in primes]) ''' Just have to get i as small as possible where the first printed value is less than the second. Answer is the third. ''' i = 4 print float(i * phin) / (i * n - 1) print float(15499) / 94744 print i * n for (p1, p2) in combinations(primes, 2): potential_n = n * p1 phi_potential_n = p2 * phin * p1 / (p1 - 1) if float(phi_potential_n) / (potential_n - 1) < float(15499) / 94744: print 'wtf'
# I could probably improve this to not recalculate n(v) over and over but instead build it all at once # But I had basically already written n(v) for solving the earlier coin problem, and it was easy to reuse from helpers import sieve primes = sieve(100) def n(v): ways = [1]+[0]*v for prime in primes: for i in range(prime, v+1): ways[i] += ways[i-prime] return ways[v] v = 2 while n(v) < 5000: v += 1 print v, n(v)
assert n > m answer = 0 n = [i for i in str(n)] m = [i for i in str(m)] while len(n) > len(m): answer += num_transitions[int(n[0])] n = n[1:] for i in range(len(n)): num1 = int(n[i]) num2 = int(m[i]) answer += num_transition_paired[(num1, num2)] return answer def max_transitions_required(n): answer = 0 answer += sum([num_transitions[int(i)] for i in str(n)]) while digit_sum(n) != n: answer += smart_transitions_required(n, digit_sum(n)) n = digit_sum(n) answer += sum([num_transitions[int(i)] for i in str(n)]) return answer primes = [p for p in sieve(2 * 10 ** 7) if p > 10 ** 7] print 'primes found' answer = 0 for p in primes: answer += sam_transitions_required(p) - max_transitions_required(p) print answer # print sam_transitions_required(137) # print max_transitions_required(137)
from helpers import prime_factorizations, sieve from operator import mul from sys import setrecursionlimit # bold call but you gotta do what you gotta do to not rewrite your shitty recursive solution into one using a stack :^)^)^)^)^)^)^)^)^) setrecursionlimit(10**4) N = 120000 prime_factorizations = prime_factorizations(N) primes = sieve(N) # print prime_factorizations def rad(n): return reduce(mul, prime_factorizations[n].keys()) def abc_hit(a, b, c): rad_factors = set(prime_factorizations[c].keys()) b_factors = set(prime_factorizations[b].keys()) a_factors = set(prime_factorizations[a].keys()) rad_factors = rad_factors.union(b_factors) rad_factors = rad_factors.union(a_factors) return reduce( mul, rad_factors) < c and a_factors.intersection(b_factors) == set() def possible_nums(prime_candidates, max_num, min_index):
from helpers import sieve def poly(n, a, b): return n**2 + a * n + b a_max = 1000 b_max = 1000 known_max = 81 largest_possible = poly(known_max, a_max, b_max) primes = sieve(largest_possible) prime_mask = [False] * largest_possible for p in primes: prime_mask[p] = True max = 0 max_a = None max_b = None for a in range(-a_max, a_max): for b in range(-b_max, b_max): n = 0 while True: p = poly(n, a, b) if not prime_mask[p]: break else: n += 1 if n > max: print "%s primes for %s, %s" % (n, a, b)
# This is really dumb and takes forever to run, I know # A smarter way would just check random combos matching ([3,7,9][1,3,7,9]*[3,7,9]) until you find 11 from helpers import sieve, is_prime print sum([int(p) for p in map(str,sieve(1000000)[4:]) if all([is_prime(int(p[i:])) and is_prime(int(p[:i+1])) for i in range(len(p))])])
from helpers import sieve lim = 50000000 primes = sieve(int(lim**0.5) + 1) l = [] for p1 in primes: for p2 in primes: if (p1**2 + p2**3 > lim): break for p3 in primes: if (p1**2 + p2**3 + p3**4 > lim): break l.append(p1**2 + p2**3 + p3**4) l = list(set(l)) print(len(l))