## Euler #3

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

### Euler #3

Hey everyone, i have a problem with my code to solve Project Euler #4: ( not #3! )
Code: Select all
`L = 0for x in range(100, 1000):   for y in range(100, 1000):           product = x*y          if str(product) == str(product)[::-1]:         L = productprint L`

I know it prints out the wrong palindromic because it is the last palindromic from the loops. How do i find the largest one though? a print max(L) does not work
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)

### Re: Euler #3

Append all the palindromes you find to a list then use max to find the largest?
I can't come to bed...
Someone is WRONG on the internet

http://xkcd.com/386/

jgreen45
Poster

Posts: 107
Joined: Wed Feb 25, 2009 6:18 pm
Blog: View Blog (0)

### Re: Euler #3

Your indentation was outright wrong. You should always use four spaces and absolutely no tabs. If you haven't already, I suggest reading PEP 8.

To address your problem, it always printed out the result of the last sequence because you wire directly assigning the result to L each time. So, every time the loop ran, the old result was forgotten. I fixed this by making L into a list, and appending the result each loop. That way, all the results are remembered.
Code: Select all
`L = []for x in range(100, 1000):    for y in range(100, 1000):        product = x*y        if str(product) == str(product)[::-1]:            L.append(product)print max(L)`
"Well it isn't my fault. I shouldn't have been allowed to do something to crash it." "No, you shouldn't have been allowed to buy a computer in the first place"
Phantom Wolf
Poster

Posts: 274
Joined: Wed Mar 03, 2010 8:45 pm
Blog: View Blog (0)

### Re: Euler #3

Ah thanks! That makes perfect sense.
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)