Euler #3

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

Euler #3

Post by Reason7194 on Sun Jul 31, 2011 8:06 am
([msg=60370]see Euler #3[/msg])

Hey everyone, i have a problem with my code to solve Project Euler #4: ( not #3! )
Code: Select all
L = 0
for x in range(100, 1000):
   for y in range(100, 1000):
           product = x*y
          if str(product) == str(product)[::-1]:
         L = product
print 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
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Euler #3

Post by jgreen45 on Sun Jul 31, 2011 1:05 pm
([msg=60373]see Re: Euler #3[/msg])

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/
User avatar
jgreen45
Poster
Poster
 
Posts: 106
Joined: Wed Feb 25, 2009 6:18 pm
Blog: View Blog (0)


Re: Euler #3

Post by Phantom Wolf on Sun Jul 31, 2011 1:25 pm
([msg=60378]see Re: Euler #3[/msg])

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
Poster
 
Posts: 271
Joined: Wed Mar 03, 2010 8:45 pm
Blog: View Blog (0)


Re: Euler #3

Post by Reason7194 on Sun Jul 31, 2011 4:54 pm
([msg=60381]see Re: Euler #3[/msg])

Ah thanks! That makes perfect sense.
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests