Problem 37

completed December 8, 2011

Comments

I just think it is so awesome when I think I’m still testing out a script and all of a sudden I find the correct answer has just popped out! That’s totally what happened with this script. Very awesome. I also feel like I’m really starting to get the hang of these string methods. I’m pretty quick now at figuring out how to change numbers into digits and a string of digits back to numbers. That has been a very useful skill in these last few problems.

Code

#evaluate if a number is prime or not
def isprime(n):
	if n == 1: return 'FALSE'
	for i in range(2, int(n**0.5 + 1)):
		if n%i == 0: return 'FALSE'
	return 'TRUE'
	
#define right truncation
def rtrunk(n):
	return int(str(n)[:-1])

#define left truncation
def ltrunk(n):
	return int('0' + str(n)[1:])
	

from math import log10
nlist = []
n = 10
while len(nlist) < 11:
	test = 'TRUE'
	if isprime(n) == 'TRUE':
		rn = ln = n
		for r in range(int(log10(n))):
			rn = rtrunk(rn)
			if isprime(rn) == 'FALSE': test = 'FALSE'
		for l in range(int(log10(n))):
			ln = ltrunk(ln)
			if isprime(ln) == 'FALSE': test = 'FALSE'
		if test == 'TRUE': nlist.append(n)
	n += 1

print sum(nlist)