Project Euler Problem 4 Solution in Python

Do you need “Project Euler Problem 4 Solution Python” . We will discuss all the problems in Project Euler and try to solve them using Python. I have solved Project Euler Problem 3 Python as well.

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

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

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to calculate the largest palindrome number that is created by multiplying two 3 digit numbers.

Project Euler Problem 4 Solution Python | Lets start! 

To be a palindrome number the product answer must be a 6 digit number. So we will brute force from such numbers which result in 6 digit product answer by multiplying the range 100-999 to itself. We then check by converting the resulting number into string and checking if the length is 6.

Then we match first three digits with last three digits in reverse order. If all the three matches we will add the number into a list of palindromes.

At the end we will use Max() function to determine the largest palindrome.

palindromes = []
for x in range(100,999):
    for i in range(100,999):
        num = str(i*x).split()[0]
        if len(num) == 6:
            if num[0] == num[-1] and num[1] == num[-2] and num[2] == num[-3]:
                palindromes.append(i * x)
print max(palindromes)

Now lets display the number. It show that 906609 is the largest palindrome number created by product of two 3 digit numbers.

Lets see if we got it right.

project euler problem 4 solution python
Project Euler Problem 4 Solution

Yaay! We got this right. Thanks for reading.

project euler problem 5 solution python

Happy coding!

%d bloggers like this: