I cannot understand why the value of "palindrom" changes works when the condition
if(palindrome(i*j) ):
is replaced with
if (palindrome(i*j) and i*j>palindrom)):
in this code
def palindrome(n):
return str(n)==str(n)[::-1];
def ndigitPalindrome(n):
lbound=10**(n-1);
ubound=10**(n);
palindrom=0;
for i in range(lbound,ubound):
for j in range(lbound,ubound):
if(palindrome(i*j) ):
palindrom=i*j;
return palindrom;
print "Largest n digit palindrome : ",ndigitPalindrome(3);
Aucun commentaire:
Enregistrer un commentaire