Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

euler
problem
palindrome
lagest palindrome
product

Project Euler problem 4: Largest palindrome product

Educative Answers Team

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)) 

RELATED TAGS

euler
problem
palindrome
lagest palindrome
product
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring