Re: [Tutor] largest palindrome number
If you take a close look at my code. for i in range (1,100) : for j in range (i,100) : Temp = palindrome(i*j) here, as the loop goes on, i*j can never become smaller in any case. which is why I think it, as long as PNum gets a new number, its bigger palindrome than the previous.. so, at the end of the loop. we'll get largest palindrome number.. On 8/25/11, Hugo Arts hugo.yo...@gmail.com wrote: On Thu, Aug 25, 2011 at 6:49 PM, surya k sur...@live.com wrote: Hi, I'm doing a puzzle where it asked me to find the largest palindrome number formed by the product of two three-digit numbers. They mentioned an example saying that 9009 is the largest palindrome number formed by two two-digit numbers (99 * 91). I've written my code this way.. and I tested it with the given example and I got it right! Logic I used : largest two digit number is 99 and three digit number is 999.. so largest product of two two-digit numbers is 100*100 and for three-digit numbers is 1000*1000. So, I used a for loop and it assigns a palindromic value to PNum till it is 100*100 (for 2 digit number) and 1000*1000 (for three-digit number).. Thus it stops at the max possible palindromic value, which is what we want. def palindrome (n) : TempN = n rev = 0 while n != 0 : k = n % 10 rev = (rev * 10) + k n = n / 10 if TempN == rev : return TempN # Palindrome else : return 0 # not Palindrome for i in range (1,100) : for j in range (i,100) : Temp = palindrome(i*j) if Temp 1 and Temp != 0 : PNum = Temp print PNum So, for getting the largest palindrome number formed by two three-digit numbers, I changed 100 to 1000 and 1,00,00 to 1,000,000 in the highlighted area. Thus I got the answer to be 88. When I submitted the answer, its saying wrong! Where I'm going wrong ? help me, please ! When you get a new palindrome, you should make sure it's bigger than the one you already have before you replace the old one. 88 is the last palindrome you find, but it is not the the biggest. You cannot assume the biggest one will be found last. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest palindrome number
On 8/27/2011 8:21 AM surya k said... If you take a close look at my code. for i in range (1,100) : for j in range (i,100) : Temp = palindrome(i*j) here, as the loop goes on, i*j can never become smaller Of course it can... i=3 * j=90 is less than i=4 * j=4... Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest palindrome number
surya k wrote: If you take a close look at my code. for i in range (1,100) : for j in range (i,100) : Temp = palindrome(i*j) here, as the loop goes on, i*j can never become smaller in any case. which is why I think it, as long as PNum gets a new number, its bigger palindrome than the previous.. so, at the end of the loop. we'll get largest palindrome number.. Add a print statement to your loop for i in range(1, 1000): for j in range(i, 1000): temp = palindrome(i*j) if temp != 0: print temp, i, j pnum = temp print pnum and you'll see the problem: ... 828828 897 924 819918 902 909 824428 902 914 906609 913 993 -- 886688 916 968 861168 924 932 88 924 962 88 You get the palindrome with the largest factor i which is not necessarily the one with the largest product i*j. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest palindrome number
* surya k sur...@live.com [2011-08-27 20:51]: snip here, as the loop goes on, i*j can never become smaller in any case. which is why I think it, as long as PNum gets a new number, its bigger palindrome than the previous.. so, at the end of the loop. we'll get largest palindrome number.. On 8/25/11, Hugo Arts hugo.yo...@gmail.com wrote: snip When you get a new palindrome, you should make sure it's bigger than the one you already have before you replace the old one. 88 is the last palindrome you find, but it is not the the biggest. You cannot assume the biggest one will be found last. As implied by Hugo, What you need to to do is store your largest current palindrome and as you find a new one, replace the largest current only if the one you just found is actually larger. Once you go through the entire block, the value in your largest current value will actually be the largest palindrome. -- David Rock da...@graniteweb.com pgpqe98kqh5z1.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest palindrome number
On Thu, Aug 25, 2011 at 6:49 PM, surya k sur...@live.com wrote: Hi, I'm doing a puzzle where it asked me to find the largest palindrome number formed by the product of two three-digit numbers. They mentioned an example saying that 9009 is the largest palindrome number formed by two two-digit numbers (99 * 91). I've written my code this way.. and I tested it with the given example and I got it right! Logic I used : largest two digit number is 99 and three digit number is 999.. so largest product of two two-digit numbers is 100*100 and for three-digit numbers is 1000*1000. So, I used a for loop and it assigns a palindromic value to PNum till it is 100*100 (for 2 digit number) and 1000*1000 (for three-digit number).. Thus it stops at the max possible palindromic value, which is what we want. def palindrome (n) : TempN = n rev = 0 while n != 0 : k = n % 10 rev = (rev * 10) + k n = n / 10 if TempN == rev : return TempN # Palindrome else : return 0 # not Palindrome for i in range (1,100) : for j in range (i,100) : Temp = palindrome(i*j) if Temp 1 and Temp != 0 : PNum = Temp print PNum So, for getting the largest palindrome number formed by two three-digit numbers, I changed 100 to 1000 and 1,00,00 to 1,000,000 in the highlighted area. Thus I got the answer to be 88. When I submitted the answer, its saying wrong! Where I'm going wrong ? help me, please ! When you get a new palindrome, you should make sure it's bigger than the one you already have before you replace the old one. 88 is the last palindrome you find, but it is not the the biggest. You cannot assume the biggest one will be found last. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor