Problem 35

completed December 6, 2011

Comments

Again, I really must learn how to follow directions. Spent way too much time getting the wrong answer when all I had to do was read the instructions more closely.

Code

import re
def num2str(n):
	return re.split('_', re.sub('\B', '_', str(n)))

def str2num(n):
	return ''.join(map(str, n))	

def circle(n):
	return int(str2num([(list(num2str(n)))[-1]] + (list(num2str(n)))[:-1]))
	
def isprime(n):
	for i in range(2, int(n**0.5 + 1)):
		if n%i == 0: return 'FALSE'
	return 'TRUE'
			
from math import log10
circleprimes = []
for n in range(2, 10**6):
	if isprime(n) == 'TRUE':
		m = n
		circlelist = [m]
		for x in range(int(log10(m))):
			m = circle(m)
			circlelist.append(m)
		if 'FALSE' not in map(isprime, circlelist): 
			circleprimes.append(n)
			
print circleprimes		
print len(circleprimes)