def diagonal_members(size): diagonals = set() for i in range(size, 0,-2): # print("Layer ", i) for j in range(0,4): # print(i, j, i*i - (i-1)*j) diagonals.add(i*i - (i-1)*j) number_of_primes = sum(1 for x in diagonals if MathFunctions.miller_is_prime(x)) # print(number_of_primes,len(diagonals)) return (number_of_primes/len(diagonals))
''' 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? ''' import MathFunctions, time def is_pandigital(num): str_rep = str(num) string_rep_set = set(i for i in str_rep) if str_rep.__contains__('0'): return False length = len(str_rep) if length!= len(set(str_rep)): return False pan_digits = set(range(1,length+1)) num_digits = set(ord(i) - ord('0') for i in str_rep) return num_digits.difference(pan_digits) == set() start_time = time.time() # cannot have 9 because 1+2+..+9 = 45, divisible always by 3 and 9, thus not a prime. Same for 8, sum is 36. for i in range(7654321, 1, -2): if is_pandigital(i) and MathFunctions.miller_is_prime(i): print(i) break print("--- %s seconds ---" % (time.time() - start_time))
''' The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? ''' import MathFunctions, itertools def is_permuted(a, b): list_of_permutations =[int(''.join(x)) for x in itertools.permutations(str(a))] return list_of_permutations.__contains__(b) for i in range(1489, 10000): if MathFunctions.miller_is_prime(i) and MathFunctions.miller_is_prime(i+3330) and MathFunctions.miller_is_prime(i+6660)\ and is_permuted(i,i+3330) and is_permuted(i, i+6660): print(i, i+3330, i+6660) break
def sumOfPrimesBelow(upperLimit): sum = 2 for i in range(3, upperLimit, 2): if MathFunctions.miller_is_prime(i): sum+=i return sum