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: power += 1 n /= p if power > 0: ans[p] = power if isPrimeMR(n): ans[n] = 1 elif n > 1: recursive_ans = {k : v for (k, v) in pfs[n].items()} for (prime, power) in ans.items(): if prime in recursive_ans: recursive_ans[prime] += power else: recursive_ans[prime] = power return recursive_ans return ans
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: power += 1 n /= p if power > 0: ans[p] = power if isPrimeMR(n): ans[n] = 1 elif n > 1: recursive_ans = {k: v for (k, v) in pfs[n].items()} for (prime, power) in ans.items(): if prime in recursive_ans: recursive_ans[prime] += power else: recursive_ans[prime] = power return recursive_ans return ans
def B(x, y, n): total = 0 for p in range(x, x + y + 1): if p % 100000 == 0: print p if isPrimeMR(p): total += fast_a(n, p) 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
Once we have this, answer is sum of (a + b choose b). ''' from helpers import isPrimeMR, modinv from math import sqrt, ceil modulus = 1000000007 primes = [] low_limit = 10**7 high_limit = 10**7 + 10**4 for candidate in range(low_limit, high_limit): if isPrimeMR(candidate): primes.append(candidate) print 'primes found, there are %s of them ' % len(primes) k_facts = [1] + [0] * (high_limit - 1) cumprod = 1 for k in range(1, high_limit): cumprod *= k cumprod %= modulus k_facts[k] = cumprod k_facts_invs = [modinv(k, modulus) for k in k_facts] def binom(n, k, modulus):
else: return False return True n = 14 right_truncatable_harshad_nums = [[], [i for i in range(1, 10)], [i for i in range(10, 100) if right_truncatable_harshad_number(i)]] i = 2 for i in range(2, n - 1): nums = [] for right_truncatable_harshad_num in right_truncatable_harshad_nums[-1]: for last_digit in range(10): candidate = last_digit + 10 * right_truncatable_harshad_num if candidate % digit_sum(candidate) == 0: nums.append(candidate) right_truncatable_harshad_nums.append(nums) total = 0 for j in range(1, n): strong_right_truncatable_harshad_nums = [i for i in right_truncatable_harshad_nums[j] if isPrimeMR(i / digit_sum(i))] for num in strong_right_truncatable_harshad_nums: for last_digit in range(10): candidate = last_digit + 10 * num if isPrimeMR(candidate): total += candidate print total
from helpers import isPrimeMR from itertools import permutations primes = [] # No pandigital 9 digit primes b/c they're all divisible by 3. for num_digits in range(1, 9): print num_digits for p in permutations('123456789', num_digits): if isPrimeMR(int(''.join(p))): primes.append(int(''.join(p))) print len(primes) class PrimeCounter: def __init__(self): self._index = 0 self.mpdigits_numprimes = {} def increase_digitcount(self, digitset): if digitset in self.mpdigits_numprimes: (index, count) = self.mpdigits_numprimes[digitset] self.mpdigits_numprimes[digitset] = (index, count + 1) else: self.mpdigits_numprimes[digitset] = (self._index, 1) self._index += 1 def digitset(n): return frozenset([i for i in str(n)]) pc = PrimeCounter()
from helpers import isPrimeMR N = 50000000 total = 0 for k in xrange(2, N + 1): if isPrimeMR(2 * k * k - 1): total += 1 print total
from helpers import isPrimeMR hamming_number_factor_start = [2, 3, 5] limit = 10**12 allowable_primes = [] p2 = 0 while 2**p2 < limit: p3 = 0 while 2**p2 * 3**p3 < limit: p5 = 0 while 2**p2 * 3**p3 * 5**p5 < limit: num = 2**p2 * 3**p3 * 5**p5 if isPrimeMR(num + 1): allowable_primes.append(num + 1) p5 += 1 p3 += 1 p2 += 1 allowable_primes.remove(2) allowable_primes.remove(3) allowable_primes.remove(5) allowable_primes = sorted(allowable_primes) allowable_primes = [i for i in reversed(allowable_primes)] print allowable_primes def S_with_given_235(num, list_to_consider):
def pseudofortunate(n): count = 2 while not isPrimeMR(n + count): count += 1 return count
n = 14 right_truncatable_harshad_nums = [[], [ i for i in range(1, 10) ], [i for i in range(10, 100) if right_truncatable_harshad_number(i)]] i = 2 for i in range(2, n - 1): nums = [] for right_truncatable_harshad_num in right_truncatable_harshad_nums[-1]: for last_digit in range(10): candidate = last_digit + 10 * right_truncatable_harshad_num if candidate % digit_sum(candidate) == 0: nums.append(candidate) right_truncatable_harshad_nums.append(nums) total = 0 for j in range(1, n): strong_right_truncatable_harshad_nums = [ i for i in right_truncatable_harshad_nums[j] if isPrimeMR(i / digit_sum(i)) ] for num in strong_right_truncatable_harshad_nums: for last_digit in range(10): candidate = last_digit + 10 * num if isPrimeMR(candidate): total += candidate print total
hamming_number_factor_start = [2, 3, 5] limit = 10**12 allowable_primes = [] p2 = 0 while 2**p2 < limit: p3 = 0 while 2**p2 * 3**p3 < limit: p5 = 0 while 2**p2 * 3**p3 * 5**p5 < limit: num = 2**p2 * 3**p3 * 5**p5 if isPrimeMR(num + 1): allowable_primes.append(num + 1) p5 += 1 p3 += 1 p2 += 1 allowable_primes.remove(2) allowable_primes.remove(3) allowable_primes.remove(5) allowable_primes = sorted(allowable_primes) allowable_primes = [i for i in reversed(allowable_primes)] print allowable_primes def S_with_given_235(num, list_to_consider):
Letting alpha = x - y and beta = x + y, we can further transform this to beta^2 = alpha^2(4alpha - 3), where the prime is 2 * alpha - 1. Since x > y, we have alpha in the range from 1 to (limit + 1) / 2 inclusive But this still gives linear runtime. So to get the sqrt(n) runtime required for the given limit, we only iterate over odd squares for a given odd value of i, set i^2 = 4alpha - 3. Then alpha can be at most (limit + 1) / 2, so 4alpha - 3 is at most 2 * (limit + 1) - 3 or 2 * limit - 1 4037526 [Finished in 1651.7s] ''' from math import sqrt from helpers import isPrimeMR limit = 5 * 10**15 count = 0 for i in xrange(1, int(sqrt(2 * limit)), 2): if i % 100000 == 1: print i, int(sqrt(2 * limit)) alpha = (i * i + 3) / 4 if isPrimeMR(2 * alpha - 1): # print 2 * alpha - 1 count += 1 print count
from helpers import sieve, isPrimeMR from itertools import product, combinations number_len = 10 answer = 0 for digit in range(10): num_replacements = 1 other_digits = [str(i) for i in range(10)] other_digits.remove(str(digit)) primes_not_found = True while primes_not_found: # Generate order digits to go in the number for perm in product(other_digits, repeat = num_replacements): # Then generate combinations for which places those digits go for comb in combinations(range(number_len), num_replacements): l = [str(digit)] * number_len for i in range(num_replacements): l[comb[i]] = perm[i] possible_prime = int(''.join(l)) if possible_prime > 10**(number_len - 1): if isPrimeMR(possible_prime): primes_not_found = False print possible_prime answer += possible_prime num_replacements += 1 print answer