Project Euler problem 4: Largest palindrome product
In this problem, you have to calculate the largest palindrome number that can be made from the product of two n-digit integers.
Algorithm
- Calculate the upper and lower bound (i.e., for n = 2, lowerBound = 10 and upperBound = 99 )
- Search for the highest product palindrome in reverse order (i.e., start a loop from 99 * 99 )
- Check whether or not each product is a palindrome.
Code
# Python Implementation# functiondef PalindromeProductNumber(n):upperBound = 0lowerBound = 0# Calculating upper boundfor i in range(1, n+1):upperBound =upperBound * 10upperBound =upperBound + 9# Calculating lower boundlowerBound = 1 + lowerBound//10max_prdt = 0for i in range(upperBound,lowerBound-1, -1):for j in range(i,lowerBound-1,-1):# calculating products in a rangeproduct = i * jif (product < max_prdt):breaknumber = productreverse_num = 0# checking whether a number is palindromewhile (number != 0):reverse_num = reverse_num * 10 + number % 10number =number // 10# updating productif (product == reverse_num and product > max_prdt):max_prdt = productreturn max_prdtn = 2print(PalindromeProductNumber(n))
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved