DAY 28 of PYTHON top 100 questions : from Basic to Advanced !!

DAY 28 of PYTHON top 100 questions : from Basic to Advanced !!

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
  1. def is_palindrome(number):: This line defines a function named is_palindrome that takes a number as input.

  2. 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 returns True, indicating that the number is a palindrome. Otherwise, it returns False.

  3. def largest_palindrome_product(n):: This line defines another function named largest_palindrome_product, which takes an integer n as input.

  4. if n <= 0:: This line checks if the input n is less than or equal to zero. If n is not a positive integer, the function returns None, indicating that the input is not valid.

  5. lower_bound = 10**(n - 1): This line calculates the lower bound for n-digit numbers. For example, if n is 2, this sets lower_bound to 10 (the smallest two-digit number).

  6. upper_bound = 10**n - 1: This line calculates the upper bound for n-digit numbers. For example, if n is 2, this sets upper_bound to 99 (the largest two-digit number).

  7. largest_palindrome = 0: This line initializes a variable largest_palindrome to 0, which will be used to store the largest palindrome found.

  8. for i in range(upper_bound, lower_bound - 1, -1):: This line starts a loop that iterates through the range of values from upper_bound down to lower_bound - 1, in descending order.

  9. 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 of i down to lower_bound - 1, in descending order. This is used to generate products of two n-digit numbers.

  10. product = i * j: This line calculates the product of the current values of i and j.

  11. if product < largest_palindrome:: This line checks if the product is less than the largest_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 this i. This is an optimization to reduce unnecessary computations.

  12. break: If the product is smaller than the largest_palindrome, it breaks out of the inner loop and continues with the next value of i.

  13. if is_palindrome(product):: This line checks if the product is a palindrome using the is_palindrome function defined earlier.

  14. largest_palindrome = product: If the product is a palindrome and it's larger than the largest_palindrome found so far, it updates the largest_palindrome with this new value.

  15. return largest_palindrome: After both loops have been completed, the function returns the largest palindrome found.

  16. n = int(input("Enter the number of digits (n): "): This line takes user input to specify the number of digits (n).

  17. result = largest_palindrome_product(n): This line calls the largest_palindrome_product function with the user-provided value of n and stores the result in the result variable.

  18. if result:: This line checks if a valid result was obtained (i.e., if result is not None).

  19. 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.

  20. else:: If the input n was not a positive integer (i.e., result is None), this line is executed.

  21. 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 !!!