Skip to main content

Euler 0036

The Problem:

CircularFind primesthe sum of all numbers, less than one million, which are primespalindromic thatin canbase be caesar'd10 and stillbase be the prime all the way through.

Find how many circular primes there are below 1 million.2.

Considerations and Approach:

We are going to generatelook allat theevery primesnumber under 1000000, then iterate through all of them.
For each prime we will rotate it by it's length -between 1 and ifthe everyupper rotationlimit, isturn it into a prime,string thenand verify if it is a circularpalindrome primein base 10.

If it is then we convert the number into a binary string representation (which we take off the first two letters of) and we check if that is palindromic.

If it is, then we add the number to the list of palindromes.

Then at the end we sum up the list of palindromes.

The Code:

import math

prime_thresholdupper_limit = 1000000

number_list = list(range(2, prime_threshold+1))
prime_listpals = []

current = 0
total_iteration = 0
while current < len(number_list):
    if number_list[current] != -1:
        prime = number_list[current]
        prime_list += [prime]
        
        increment = current + prime
        while increment < len(number_list):
            number_list[increment] = -1
            increment += prime
            total_iteration += 1
        
        #print(number_list)
        #print(prime_list)
    current += 1

print(len(prime_list))

circ_primes = [2]

prime_list = [prime for prime in prime_list if '0' not in str(prime) and '2' not in str(prime) and '4' not in str(prime) and '6' not in str(prime) and '8' not in str(prime)]

print(len(prime_list))

iterations = 0
for prime in prime_list:
    iterations += 1
    circular = True
    current = prime
for i in range(1,int(math.log(prime, 10))+1)upper_limit):
    currentstr_i = int(str(current)[-1] + str(current)[0:-1])
        #print(prime, current)
        #print(prime, current)i)
    if currentstr_i not== instr_i[::-1]:
        prime_list:
            circularbin_i = False
            breakbin(i)[2:]
        if circular:bin_i circ_primes== bin_i[::-1]:
            print(i, bin_i)
            pals += [prime]

    if iterations % 10000 == 0:
        print(iterations/len(prime_list))

#print(circ_primes)i]

print(len(circ_primes)pals), sum(pals))