def main(n): # Returns the nth prime number. counter = 0 testnum = 1 while counter < n: testnum = Euler.nextPrime(testnum) counter += 1 return testnum
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 counter = 0 answer = 0 permutations = [] while answer == 0: counter = Euler.nextPrime(counter) for x in range(10): if digitCount(counter,x) > 1: if len(permutePrimes(counter, x)) > 7: permutations = permutePrimes(counter, x) answer = permutations[0] print("The answer is " + str(answer)) print("The primes are: " + str(permutations)) t2 = time.clock() print("Execution time = " + str(t2-t1)[:5] + " seconds")
# Problem 46: Goldbach's Other Conjecture # Find the smallest odd composite number that cannot be written as # the sum of a prime and twice a square. import time, Euler t1 = time.clock() primes = [2,3,5,7,11] candidate = 9 answer = 0 while True: if candidate == primes[-1]: primes.append(Euler.nextPrime(candidate)) candidate += 2 else: answer_found = False goldbach = False for prime in reversed(primes[:-1]): counter = 1 while goldbach == False: result = (prime + (2 * (counter**2))) if result == candidate: goldbach = True candidate += 2 elif result < candidate: counter += 1 else: break if goldbach == True: break
composite = large*small # Deal with the case where composite is less than limit if composite < limit: phi = composite - (large + small - 1) # Put the composite in the dictionary if it's a permutation if (int("".join(sorted([x for x in str(composite)]))) == int("".join(sorted([x for x in str(phi)])))): composites[composite] = [large, small, phi] # Increase large and add it to primes if necessary large_prime_counter +=1 if large_prime_counter not in primes: primes[large_prime_counter] = Euler.nextPrime(primes[large_prime_counter-1]) large = primes[large_prime_counter] # Deal with the case where composite is too big else: small_prime_counter -= 1 large_prime_counter = small_prime_counter +1 primes[small_prime_counter] = Euler.previous_prime(primes[small_prime_counter+1]) small = primes[small_prime_counter] large = primes[large_prime_counter] min_ratio = 3 answer = 0 for key, value in composites.items(): n = key phin = value[2]