Write a Python program to find the largest palindrome made from the product of two n-digit numbers.
#Write a Python program to find the largest palindrome made from the product of two n-digit numbers.
def is_palindrome(number):
return str(number) == str(number)[::-1]
def largest_palindrome_product(n):
if n <= 0:
return None
lower_bound = 10**(n - 1)
upper_bound = 10**n - 1
largest_palindrome = 0
for i in range(upper_bound, lower_bound - 1, -1):
for j in range(i, lower_bound - 1, -1):
product = i * j
if product < largest_palindrome:
break
if is_palindrome(product):
largest_palindrome = product
return largest_palindrome
n = int(input("Enter the number of digits (n): "))
result = largest_palindrome_product(n)
if result:
print(f"The largest palindrome made from the product of two {n}-digit numbers is: {result}")
else:
print("Please enter a positive integer for n.")
Output :
Enter the number of digits (n): 8
The largest palindrome made from the product of two 8-digit numbers is: 9999000000009999
def is_palindrome(number):
: This line defines a function namedis_palindrome
that takes a number as input.return str(number) == str(number)[::-1]
: In this function, it converts the input number to a string, and then it checks if the string is equal to its reverse (achieved by slicing with[::-1]
). If the string representation of the number is the same forwards and backward, the function returnsTrue
, indicating that the number is a palindrome. Otherwise, it returnsFalse
.def largest_palindrome_product(n):
: This line defines another function namedlargest_palindrome_product
, which takes an integern
as input.if n <= 0:
: This line checks if the inputn
is less than or equal to zero. Ifn
is not a positive integer, the function returnsNone
, indicating that the input is not valid.lower_bound = 10**(n - 1)
: This line calculates the lower bound for n-digit numbers. For example, ifn
is 2, this setslower_bound
to 10 (the smallest two-digit number).upper_bound = 10**n - 1
: This line calculates the upper bound for n-digit numbers. For example, ifn
is 2, this setsupper_bound
to 99 (the largest two-digit number).largest_palindrome = 0
: This line initializes a variablelargest_palindrome
to 0, which will be used to store the largest palindrome found.for i in range(upper_bound, lower_bound - 1, -1):
: This line starts a loop that iterates through the range of values fromupper_bound
down tolower_bound - 1
, in descending order.for j in range(i, lower_bound - 1, -1):
: Inside the outer loop, this line starts an inner loop that iterates through the same range of values as the outer loop, but starting from the current value ofi
down tolower_bound - 1
, in descending order. This is used to generate products of two n-digit numbers.product = i * j
: This line calculates the product of the current values ofi
andj
.if product < largest_palindrome:
: This line checks if theproduct
is less than thelargest_palindrome
found so far. If it is, it means we've already found a palindrome greater than this product, so there's no need to continue checking products for thisi
. This is an optimization to reduce unnecessary computations.break
: If the product is smaller than thelargest_palindrome
, it breaks out of the inner loop and continues with the next value ofi
.if is_palindrome(product):
: This line checks if theproduct
is a palindrome using theis_palindrome
function defined earlier.largest_palindrome = product
: If the product is a palindrome and it's larger than thelargest_palindrome
found so far, it updates thelargest_palindrome
with this new value.return largest_palindrome
: After both loops have been completed, the function returns the largest palindrome found.n = int(input("Enter the number of digits (n): ")
: This line takes user input to specify the number of digits (n).result = largest_palindrome_product(n)
: This line calls thelargest_palindrome_product
function with the user-provided value ofn
and stores the result in theresult
variable.if result:
: This line checks if a valid result was obtained (i.e., ifresult
is notNone
).print(f"The largest palindrome made from the product of two {n}-digit numbers is: {result}")
: If a valid result is found, it prints the result in a formatted string.else:
: If the inputn
was not a positive integer (i.e.,result
isNone
), this line is executed.print("Please enter a positive integer for n.")
: It prints a message indicating that the input was not valid.
If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).
HAPPY LEARNING !!!