Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Largest palindrome product: Euler Project Problem 4

Thursday, August 28, 2014

Largest palindrome product: Euler Project Problem 4

Largest palindrome product

Problem 4

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.

Python Code

def largestPalindromeProduct():
    largestPalin = 1
    def checkPalin(strNumb):
        palin = True
        for i in range(len(strNumb)+1/2):
            if strNumb[i] == strNumb[len(strNumb)-1-i]:
                continue
            else:
                palin = False     
        if palin == False:
            return False
        else:
            return True
    for i in range(999-100+1):
        i = i+100
        for j in range(999-100+1):
            j = j+100
            numb = i*j
            strNumb = str(numb)
            if strNumb[0] != strNumb[len(strNumb)-1]:
                continue
            if checkPalin(strNumb) == True:
                if largestPalin < numb:
                    largestPalin = numb
                    a = i
                    b = j
    print "Largest Palindrome formed by the product of three digit numbers is:",largestPalin,"=",a,"x",b
largestPalindromeProduct()           

No comments:

Post a Comment