def largest_palindrome_product(n:int) -> int:
'''
Returns largest palindrome whose a product of two n digit(base 10) integer
:param n: the number of digits in the numbers we compute the product of
:return: largest palindrome whose a product of two n digit(base 10) integer or -1 if non were found
'''
# Dealing with edge cases
if n == 1:
return 9
elif n < 1:
raise ValueError("Expecting n to be >= 1")
max_dictmul_max = {'max': -1, 'i': -1,'j': -1}
upper_boundary = (10**n) - 1
lower_boundary = 10**(n-1)
# Searching for the largest palindrome between the upper boundary and the lower one.
for i in range(upper_boundary, lower_boundary-1, -1):
for j in range(i, lower_boundary-1, -1):
# Even if this is a palindrom we will not return it therefor we dont need to do the computation in the loop
if i <= max_dict['i'] and j <= max_dict['j']:
continue
prod = i*j
str_prod = str(prodi*j)
if prodi*j > max_dict['max']mul_max and str_prod[::-1] == str_prod:
max_dict['max']mul_max = prodi*j
max_dict['i'] = i
max_dict['j'] = j
return max_dict['max']mul_max
Here'sHere is a simple Testsmall test case for the solution abovethis code:
from ProjectEuler.problem4 import largest_palindrome_product
if __name__ == "__main__":
# largest prime product is of 91*99 -> returns 9009
print(largest_palindrome_product(2))
# largest prime product isChecking ofedge 9*1cases -> returns 9
print(largest_palindrome_product(1))
# largest prime product is of 993*913 -> returns 906609
print(largest_palindrome_product(3))
Edit: Ive changed the code according to all the suggestions made by AJNeufeld, Thanks! Hope its better now. Let me know your thoughts on this solution :)