def permutePrimes(prime, character): # Replaces each instance of character in prime with 0-9 and # returns a list of each resulting number that is also prime digits = [x for x in range(10)] digits = list(filter(lambda x: x != character, digits)) answers = [prime] for x in digits: testnum = replaceDigit(prime, character, x) if Euler.isPrime(testnum) == True: if len(str(testnum)) == len(str(prime)): answers.append(testnum) return answers
def main(limit): # Iterates through each number less than limit testing for primeness. # Counter is multiplied by each prime number for the a number of times # equal to the greatest number of times that prime divides a number less # than or equal to the limit. For example, 2 divides 8 3 times (8 = 2*2*2), # so when limit == 10 counter is multiplied by 2^3. counter = 1 for x in range(2, limit+1): if Euler.isPrime(x): counter *= (x ** math.floor(math.log(limit, x))) return counter
def naiveResilience(num): if(num==1): return 0/num if(Euler.isPrime(num,Euler.currPrimes)): return (num-1)/num count = 1 for i in range(2,num): hit = False b = math.sqrt(num) for p in Euler.currPrimes: if(p>b): break if(i%p==0 and num%p==0): hit = True break if(not hit): count+=1 return count/num
# Problem 58: Spiral primes import time import Euler t1 = time.clock() diagonal = 1 primes = [] non_primes = [1] side_length = 3 ratio = 1 while ratio > 1/10: # Change to: while found = False for x in range(4): diagonal += side_length - 1 if Euler.isPrime(diagonal) == True: primes.append(diagonal) else: non_primes.append(diagonal) ratio = len(primes) / (len(primes) + len(non_primes)) side_length += 2 t2 = time.clock() print("Side length = ", side_length - 2) print(str(t2-t1)[:9])
def is_pair(x,y): # Returns True if xy and yx are each prime, else returns False test1 = int(str(x) + str(y)) test2 = int(str(y) + str(x)) return Euler.isPrime(test1) == True and Euler.isPrime(test2) == True
if is_pair(prime, candidate) == True: pairs.append(candidate) candidate -= 2 if len(pairs) > 1: answer.append((len(pairs), pairs)) candidate = Euler.previous_prime(pairs[-1]) pairs = pairs[:-1] if len(pairs) < 2: finished = True if len(answer) == 0: prime_table[prime] = False else: prime_table[prime] = answer for x in range(8, 700): if Euler.isPrime(x) == True: evaluate_prime(x) print(prime_table[13]) t2 = time.clock() print(str(t2-t1)[:5])
# Problem 50: Consecutive Prime Sum import time t1 = time.clock() import Euler limit = 4000 primes = [x for x in Euler.primeSieve(limit)] answer = [(0,[])] for x, prime1 in enumerate(primes): startprime = x sequence = [] counter = 0 for y, prime2 in enumerate(primes): if y >= x: counter += prime2 if Euler.isPrime(counter) == True: if counter < 1000000: sequentials = [x for x in primes[x:y+1]] if len(answer[0][1]) < len(sequentials): answer[0] = ((counter, sequentials)) print("Prime = " + str(answer[0][0]) + " ; Length of sequence: " + str(len(answer[0][1]))) print("Sequence start = " + str(answer[0][1][0]) + " ; Sequence end = " + str(answer[0][1][-1])) t2 = time.clock() print("Runtime = " + str(t2-t1)[:4] + " seconds")