Project Euler: Problem 4

Problem:

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.

Source: http://projecteuler.net/problem=4


Solution:

Before we can being to solved the problem above we first must answer the questions raised by it. In other words:

  • Palindromic Number?

 

Palindromic Number?

A palindromic number a number that is a palindrome, which is just a word or phrase that is the same in either direction.

 

Code:

Source: https://bitbucket.org/TWith2Sugars/project-euler/src/dbd1afaa7fc6/python/4.py

def isPalindrome(n):
    return (n==n[::-1])

def findLargestPalindrome():
    largest = 0
    for x in range(100,1000):
        for y in range(100,1000):
            product = x*y

            if product > largest and isPalindrome(str(product)):
                largest = product
    return largest

print(findLargestPalindrome())

 

IsPalindrome

This method is what is responsible for checking if a number is a palindrome; the “[::-1]” is what takes the parameter and reverses it. That’s it!

 

Leave a Reply