I solved the HackerRank version of the Largest palindrome problem (similar to Project Euler 4) in Python:
Find the largest palindrome made from the product of two 3-digit numbers which is less than N, where 101101 < N < 1000000.
How can I make this more efficient? My solution keeps timing out.
Here is the Code:
# Find the Largest palindrome made from two three digit numbers which are less than N
# Get the input and store it in an array
def get_input():
limit = int(input())
limits = []
for _ in range(limit):
limits.append(int(input().strip()))
return limits
# Check if palindrome or not
def is_palindrome(num):
if str(num) == str(num)[::-1]:
return True
else:
return False
# Find the Largest Palindrome
def largest_palindrome(num):
largest = 0
for i in range(2,999+1):
for j in range(i+1 , 999+1):
prod = i * j
if is_palindrome(prod) and prod > largest and prod < num:
largest = prod
return largest
# Get the Limits
limits = get_input()
for limit in limits:
print(largest_palindrome(int(limit)))