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.

svg viewer

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
# function
def PalindromeProductNumber(n):
upperBound = 0
lowerBound = 0
# Calculating upper bound
for i in range(1, n+1):
upperBound =upperBound * 10
upperBound =upperBound + 9
# Calculating lower bound
lowerBound = 1 + lowerBound//10
max_prdt = 0
for i in range(upperBound,lowerBound-1, -1):
for j in range(i,lowerBound-1,-1):
# calculating products in a range
product = i * j
if (product < max_prdt):
break
number = product
reverse_num = 0
# checking whether a number is palindrome
while (number != 0):
reverse_num = reverse_num * 10 + number % 10
number =number // 10
# updating product
if (product == reverse_num and product > max_prdt):
max_prdt = product
return max_prdt
n = 2
print(PalindromeProductNumber(n))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved