Project Euler Problem #4

This week’s Project Euler problem:

Find the largest palindrome made from the product of two 3-digit numbers.

This problem lets you play around with how data is represented in a computer’s memory. It’s much easier to check whether a word (or “string” to be technical) is a palindrome than to check a number so we multiply two 3-digit numbers and convert it to a string.

def ispalindrome(n):
	s = str(n)
	i,j = 0,len(s)-1
	
	while i <= j:
		if s[i] != s[j]:
			return False
		
		i = i + 1
		j = j - 1
	
	return True

x,y,z = 0,0,0

for p in range(100,1000):
	for q in range(p,1000):
		r = p*q
		if ispalindrome(r) and r>z:
			x,y,z = p,q,r

print x,y,z

The largest palindrome here is 906,609, which is the product of 913 and 993.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>