Project Euler - Largest Palindrome #4 Python
<p>I'm new to python and decided it would be a good idea to improve my coding (in general) by doing some of the challenges on project Euler. I'm currently stuck on problem 4 and i'm not sure what is going wrong (for those not in the know, problem 4 is as follows):</p>

<blockquote>
  <p>A palindromic number reads the same both ways.</p>
  
  <p>The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.</p>
</blockquote>

<p>Find the largest palindrome made from the product of two 3-digit numbers.</p>

<pre><code>x, y = 999, 999
palindrome = []
while True:
    palindrome = [i for i in str(x * y)]
    r_palindrome = palindrome[::-1]
    if palindrome == r_palindrome:
        break
    else:
        y -= 1
        if y &lt; 100: 
            y = x
            x -= 1
print x, y, palindrome
</code></pre>

<p>I seem to get the answer <code>987 * 286 = 282282</code> which feels awfully low. 
Can someone explain the best way of doing this and what my current code is doing wrong rather than just a simple "here is the code" answer please. </p>

<p>OK, just to find the solution, let's use brute force:</p>

<pre><code>&gt;&gt;&gt; prod = itertools.product(range(999,99,-1),range(999,99,-1))
&gt;&gt;&gt; palindromes = [(x*y,x,y) for x,y in prod  if str(x*y) == str(x*y)[::-1]]
&gt;&gt;&gt; max(palindromes, key=lambda t: t[0])
(906609, 993, 913)
</code></pre>

<p>Took a bit of time, but at least we have the answer. I've worked out a solution that works very quickly, and I think it takes the essense of what you were trying to achieve:</p>

<pre><code>&gt;&gt;&gt; x = 999
&gt;&gt;&gt; palindromes = []
&gt;&gt;&gt; floor = 99
&gt;&gt;&gt; while x &gt; floor:
...     for i in range(x,floor,-1):
...         product = x*i
...         candidate = str(product)
...         if candidate == candidate[::-1]:
...             palindromes.append((product,x,i))
...             floor = i
...             break
...     x -= 1
... 
&gt;&gt;&gt; palindromes
[(580085, 995, 583), (906609, 993, 913), (886688, 968, 916), (888888, 962, 924)]
&gt;&gt;&gt; 
</code></pre>

<p>Essentially, this updates the floor with the lowest number that last gave us a palindrome. We know we don't have to look lower than that each time. </p>

python