Sunday, December 1, 2013

Solution to Project Euler problem number 4 in Python

Solution

def reverse(number):
    result = 0
    while number > 0:
        result = result * 10 + (number % 10)
        number = int(number / 10)
    return result

def is_palindrome(number):
    return number == reverse(number)

largestPalindrome = 0
a = 999
while a >= 100:
    if a % 11 == 0:
        b = 999
        db = 1
    else:
        b = 990
        db = 11 
    while b >= a:
        if a*b <= largestPalindrome:
            break
        if is_palindrome(a*b):
            largestPalindrome = a*b
            b = b-db
        a = a-1
print largestPalindrome

No comments: