-------- Original Message -------- Subject: Re: [Tutor] largest palindrome number Date: Tue, 30 Aug 2011 23:24:09 +0530 From: surya k <[email protected]> To: bob gailer <[email protected]> Mr Gailer, That's an amazing way of writing palindrome function. Actually, I'm still using my old C logic's here. Thanks for sharing. On 8/30/11, bob gailer<[email protected]> wrote:
On 8/25/2011 12:49 PM, surya k 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 PalindromezTwice as fast on my computer! def palindrome (n): s = '%s' % n return s == s[::-1]for i in range (1,100) : for j in range (i,100) : Temp = palindrome(i*j) if Temp< 10000 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 888888. When I submitted the answer, its saying wrong! Where I'm going wrong ? help me, please ! _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor-- Bob Gailer 919-636-4239 Chapel Hill NC
_______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
