Problem 58

completed December 25, 2011

Comments

There was a problem earlier on that was similar to this one. It just requires making a formula for what the values of each of the diagonals will be depending on what the side length will be.

After I completed the problem, I spent a little time to try to optimize it. I got it to run in about 25 seconds. I’d have liked to have gotten it faster, but that will have to do for now :)

Code

def isprime(n):
	if n == 1: return False
	for i in range(2, int(n**.5 + 1)):
		if n%i == 0: return False
	return True

a = False
s = 3
total = 0

while a == False:
	for x in range(4):
		n = (s-2)**2 + (x+1)*s - (x+1)
		if isprime(n) == True:
			total += 1
	if float(100 * total / (2*(s-1) + 1)) < 10:
		print s
		a = True
	else:
		s += 2