I'm fairly new to Python and trying out the Project Euler challenges. I'm currently having trouble getting a nested for loop to work properly. My code looks like this:
def palindrome():
for i in range(999,317,-1):
for a in range(999,317,-1):
s = str(i*a)
if s[0] == s[5] and s[1] == s[4] and s[2] == s[3]:
return(s)
print(palindrome())
The point of the program is to find the largest possible palindrome that is the product of two three digit numbers. When I run this program, I consistently get the answer 580085. It is a palindrome, but I know it's the wrong answer.
Concrete Questions:
- Am I using/nesting for loops improperly in some way?
- Is there a way to search for palindromes without using the two loops?
for a in range(i,317,-1):and instead of returning you could add all the palindromes to a list and get the largest after both for loops