Largest palindrome which is product of two N | Function to check if a number is a Palindrome or not ; Taking the string value of the number ; Loop to check if every i - th character from beginning is equal to every ( N - i ) th char ; Function to find the largest palindrome which is a product of two N digited numbers ; Find lowerBound , upperBound for a given nDigits . for n = 2 [ 10 , 99 ] ; Result variables ; Keep p decrementing by 11 ; Find the nearest number divisible by 11 ; Keep decrementing q by 1 ; Update the result if t > r and is a palindrome ; Printing the final result ; Driver code
def isPalindrome ( x ) : NEW_LINE INDENT num = str ( x ) NEW_LINE result = True NEW_LINE i = 0 NEW_LINE j = len ( num ) - 1 NEW_LINE while ( i < j and result ) : NEW_LINE INDENT result = num [ i ] == num [ j ] NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT def find ( nDigits ) : NEW_LINE INDENT lowerBound = pow ( 10 , nDigits - 1 ) NEW_LINE upperBound = ( lowerBound * 10 ) - 1 NEW_LINE resultP = 0 NEW_LINE resultQ = 0 NEW_LINE resultR = 0 NEW_LINE for p in range ( upperBound , lowerBound , - 11 ) : NEW_LINE INDENT while ( p % 11 != 0 ) : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT for q in range ( upperBound , lowerBound , - 1 ) : NEW_LINE INDENT t = p * q NEW_LINE if ( t > resultR and isPalindrome ( t ) ) : NEW_LINE INDENT resultP = p NEW_LINE resultQ = q NEW_LINE resultR = t NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( resultR ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE find ( N ) NEW_LINE DEDENT