How do I make this code run in under 30s to find the largest palindrome that is the product of 2 numbers with the same number of digits?
def palindrome(maxInt):
pa=[]
for x in range(maxInt,0,-1):
for y in range(maxInt,0,-1):
num=x*y
if str(num) == str(num)[::-1]:
pa.append(num)
return max(pa)
maxInt
is the largest number with the specified digits. For example if you want a palindrome that is a multiple of 2 3-digit numbers you maxInt
would be 999. If you want the largest palindrome that is a multiple of 2 4-digit numbers maxInt
would be 9999. etc.
If maxInt = 9
, it should output 9.
If maxInt = 99
, it should output 9009.
So if maxInt = 999
, the programme should output 906609.
How do I make it return 99000099 for maxInt=9999
in under 30s