Largest palindrome product: Difference between revisions

Content added Content deleted
(Python example)
m (cleanup)
Line 693: Line 693:


def largestPalindrome(n):
def largestPalindrome(n):
""" find largest palindrome that is a product of two n-digit numbers """
"""
:type n: int
:rtype: int
"""
m, tail = 0, n // 2
m, tail = 0, n // 2
head = n - tail
head = n - tail
Line 712: Line 709:
it = tails(tail)
it = tails(tail)
if I!=J: it = double(it)
if I!=J: it = double(it)
peijunz for t1, t2 in it:
for t1, t2 in it:
val = (I + t1)*(J + t2)
val = (I + t1)*(J + t2)
s = str(val)
s = str(val)
if s == s[::-1] and val>m:
if s == s[::-1] and val>m:
sol = (I + t1, J + t2)
sol = (I + t1, J + t2)
m = val
m = val

if m:
if m:
print("{:2d}\t{:4d}".format(n, m % 1337), sol, sol[0] * sol[1])
print("{:2d}\t{:4d}".format(n, m % 1337), sol, sol[0] * sol[1])
return m % 1337
return m % 1337
return 0


if __name__ == "__main__":
if __name__ == "__main__":