Categories
Uncategorized

Palindromic Numbers – Python

Problem 4 from the Project Euler website.

The Problem

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is

9009 = 91 x 99

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

My Solution

First I did 999 x 999 to figure out the maximum number of digits the palindrome could have (6 digits).

Then I worked with a small range from 900 to 999 and multiplied them, printing only palindromes….

for x in range(900,999):
	for y in range(900,999):
		# Multuply all the things
		z=x*y
		# change it to string so we can pick up all the different digits
		zStr=str(z)
		# reverse the order of the last 3 digits
		zDigs=zStr[-1]+zStr[-2]+zStr[-3]
		# conditionals
		if len(zStr)==6 and zStr[:3]==zDigs:
			print x,"x",y,"=",x*y

which returns something like

...
902 x 909 = 819918
902 x 914 = 824428
909 x 902 = 819918
...

Easy stuff.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s