def all_comb_primes(l): if len(l) == 1: return True for i in range(len(l)-1): for j in range(i+1,len(l)): s1 = str(l[i]) s2 = str(l[j]) if not mtools.is_prime(int(s1+s2)): return False if not mtools.is_prime(int(s2+s1)): return False return True
def max_sum_primes(n): l = mtools.sieve_primes(n) lim = len(l) sum_max = l[lim-1] all_max = 0 s_all_max = 0 for i in range(lim-2): s_max = max = 0 np = 2 sum = l[i] + l[i+1] # print 'i=%s' % i j = i + 2 no_add = True while j < lim: while (j < lim) and (np <= all_max) and (sum <= sum_max): sum = sum + l[j] j = j + 1 np = np + 1 if (sum > sum_max) or (j > lim): if no_add: return s_all_max break if mtools.is_prime(sum): max = np s_max = sum # print 'max=%s s_max=%s' % (max, s_max) sum = sum + l[j] j = j + 1 np = np + 1 no_add = False if max > all_max: all_max = max s_all_max = s_max # print all_max, s_all_max return s_all_max
def pd(n): l = neighbors(n) l2 = [abs(n-x) for x in l] cnt = 0 for x in l2: if mtools.is_prime(x): cnt += 1 return cnt
def first_exceeds(lim): n = 5 i = 11 while True: i += 2 if mtools.is_prime(i): n += 1 if r(i, n) > lim: return n
def left_trunkable(n): p = len(str(n)) d = 10**(p - 1) while n > 10: n = n % d if not mtools.is_prime(n): return False d = d / 10 return True
def solve_n(lim): m = 1 cnt = 0 while True: p = 3*m*m + 3*m + 1 if p >= lim: return cnt if mtools.is_prime(p): cnt += 1 m += 1
def is_circular(n, l): digits_num = len(str(n)) p = 10**(digits_num-1) for i in range(0, digits_num): r = n % 10 n = n / 10 n = n + p*r if not mtools.is_prime(n): return False return True
def min_product_sum_with_start(k, start): #print 'min_product_sum_with_start(%d,%d) = ' % (k, start), i = start while True: if not mtools.is_prime(i): p, c = mtools.prime_factors(i) #i-k + len(factors) == sum(factors) if search_match(i, p, c, k): #print i return i i = i + 1
def M(n, d): i = 1 while i < n: pns = possible_nums(n, d, i) for ns in pns: ns.sort() for num in mtools.dict_perm(ns): if num[0] == 0: continue inum = mtools.combine_digits(num) if mtools.is_prime(inum): return n - i i += 1
def solve(): n = 1 pn = 0 i = 3 while True: n = n + 4 d = i - 1 s = i*i for j in range(1,4): if mtools.is_prime(s-j*d): pn = pn + 1 if n > 10*pn: return i # print i, n, pn i = i + 2 return i
def S(n, d): i = 1 prime_found = False primes_sum = 0 checked = {} while i < n and not prime_found: pns = possible_nums(n, d, i) for ns in pns: ns.sort() for num in mtools.dict_perm(ns): if num[0] == 0: continue inum = mtools.combine_digits(num) try: if checked[inum]: continue except KeyError: checked[inum] = True if mtools.is_prime(inum): prime_found = True primes_sum += inum i += 1 return primes_sum
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? ''' import time, mtools tab ={ 32: (2, 5), 64: (2, 6), 4: (2, 2), 81: (3, 4), 8: (2, 3), 9: (3, 2), 16: (2, 4) , 49: (7, 2), 25: (5, 2), 27: (3, 3), 36: (6, 2), 100: (10, 2)} primes = [x for x in range(2, 100) if mtools.is_prime(x)] def init(): global tab, primes for i in range(2, 101): v = prime_prod(i) if v: tab[i] = v print tab def prime_prod(n): p,c = mtools.prime_factors(n) if len(p) == 1 and c[0] > 1: return (p[0], c[0]) return None
def is_prime(n): return mtools.is_prime(n)
def form_prime(a, b, n): v = n*n + a*n + b if v < 0: v = abs(v) return mtools.is_prime(v)
def right_trunkable(n): while n > 10: n = n / 10 if not mtools.is_prime(n): return False return True