Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. (Please note that the palindromic number, in either base, may not include leading zeros.) ''' import Functions import time start = time.clock() sum = 0 for i in range(1, 1000000): test = i if (test % 10) != 0: test = Functions.int_to_str(test) if test == test[::-1]: test = i test = Functions.int_to_str(test,2) if test[-1] != '0': if test == test[::-1]: sum += i #print('Current sum is ',sum) print('\nThe final sum of all palindromic numbers is ',sum) Functions.runtime(start) ''' def isPalindrome(test): return (test == test[::-1])
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 print('The number of circular primes < 1e6 is ',len(circular_ls)) Functions.runtime(start)