def all_prime(number): l_to_r = range(1, len(number)-1) r_to_l = range(2-len(number), 0) for i in l_to_r: if not is_prime(int(number[i:])): return False for j in r_to_l: if not is_prime(int(number[:j])): return False return True
def main(n): count = 0 i = 1 while count != n: i += 1 if is_prime(i): count += 1 return i
def main(n): factors = [] n_sqrt = int(math.sqrt(n)) + 1 for i in range(2, n_sqrt): if n % i == 0: factors.append(i) all_factors = [] + factors for i in factors: all_factors.append(n /i) max_prime_factor = 0 for factor in all_factors: if is_prime(factor) and factor > max_prime_factor: max_prime_factor = factor print max_prime_factor
def main(n): factors = [] n_sqrt = int(math.sqrt(n)) + 1 for i in range(2, n_sqrt): if n % i == 0: factors.append(i) all_factors = [] + factors for i in factors: all_factors.append(n / i) max_prime_factor = 0 for factor in all_factors: if is_prime(factor) and factor > max_prime_factor: max_prime_factor = factor print max_prime_factor
def all_prime(alters): for alter in alters: if not is_prime(int(''.join(alter))): return False return True
""" from commons import is_prime def all_prime(alters): for alter in alters: if not is_prime(int(''.join(alter))): return False return True def circles(string): """make circle-numbers, which may be for general use later e.g. 197's circle-numbers are 971, 719""" numbers = [] current = string for i in range(len(string)-1): new = current[1:] + current[0] numbers.append(new) current = new return numbers result = [] for number in xrange(1000000): if is_prime(number): string = str(number) alters = circles(string) if all_prime(alters): result.append(number) print len(result) print result
#!/usr/bin/env python """ Calculate the sum of all the primes below two million. """ from commons import is_prime print sum([i for i in xrange(2, 2000000) if is_prime(i)])
""" We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? """ from itertools import permutations from commons import is_prime for n in range(9, 1, -1): perms = permutations(range(n, 0, -1), n) # ensure firstly output larger ones for p in perms: str_p = [str(i) for i in p] int_p = int(''.join(str_p)) if is_prime(int_p): print int_p exit(0)
l_to_r = range(1, len(number)-1) r_to_l = range(2-len(number), 0) for i in l_to_r: if not is_prime(int(number[i:])): return False for j in r_to_l: if not is_prime(int(number[:j])): return False return True mid_len = 1 middles = [''] result = [] while True: for middle in middles: for first in ['2', '3', '5', '7']: for last in ['3', '7']: number = first + ''.join(middle) + last if is_prime(int(number)) and all_prime(number): result.append(int(number)) if len(result) >= 11: print result break middles = product(['1','3','7','9'], repeat=mid_len) mid_len += 1 print sum(result)
def main(): acc = 2 for i in range(3, 2000000, 2): if is_prime(i): acc += i print acc
for b in xrange(2, 1000): # b can't be negative ### the following trick could not speed up the program significantly. ### # trans the formula to: n(n+a)+b # solve the equotion: n = +-b, n+a = -+b #possible_n = sorted((-b, b, -b-a, b-a)) #upper_limit = None #for p_n in possible_n: # if p_n > 0: # upper_limit = p_n # break #if upper_limit != None and upper_limit <= maximum[0]: # continue ### end of trick ### n = 0 while True: if is_prime(formula(a,b,n)): n += 1 else: break if n > maximum[0]: maximum = [n, a, b] # n stands for the number of primes. It's a trick. print a, b, n### print maximum