Problem 35 The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? ''' import Functions import time start = time.clock() circular_ls = [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97] primes_ls = Functions.primes_sieve(999999) num = '' for prime in primes_ls: if prime > 97: num = Functions.int_to_str(prime) # num is now a char string corresponding to the integer num_rot = len(num) -1 while(num_rot > 0): num = Functions.rotate(num) if Functions.str_to_int(num) not in primes_ls: break num_rot -=1 if num_rot == 0: circular_ls.append(prime) # if num_rot drops to zero, passed test, append to circular list
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. ''' import Functions import time start = time.clock() count = 0 sum = 0 primes_ls = Functions.primes_sieve(1000000) test_ls = primes_ls[4:] for prime in test_ls: test = str(prime) while len(test) > 0: test = test[1:] if len(test) == 0: break if int(test) not in primes_ls: break if len(test) == 0: test = str(prime) while len(test) > 0: test = test[0:-1]
n² + an + b, where |a| < 1000 and |b| < 1000 where |n| is the modulus/absolute value of n e.g. |11| = 11 and |−4| = 4 Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0. ''' import Functions import time def quad_function(n, a, b): return n**2 + a*n + b start = time.clock() primes = Functions.primes_sieve(1002000) prime_test_ls = Functions.primes_sieve(1000) n = 0 current_max = 0 current_test = 0 best_a = 0 best_b = 0 for a in range(-999, 1000): for b in prime_test_ls: while(True): if quad_function(n, a, b) in primes: current_test += 1