python - Locating the largest three-digit product - which is also a plaindrome -
def reverseinteger(x): x_string = str(x) x_list = list(x_string) x_reversedlist = reversed(x_list) x_reversedstring = "".join(x_reversedlist) x_reversed = int(x_reversedstring) return x_reversed def paliproduct(i1, i2): while i1 < 1000 , i2 < 1000: product = i1 * i2 i1 += 1 i2 += 1 if product == reverseinteger(product): return product print(paliproduct(100, 100)) i using python (which obvious)... question why shell did not try possible values of i1 i2 (100-999) , broke after conducting 1 round 100 , 100...
it returns finds palindrome because tell using return:
if product == reverseinteger(product): return product if want find of palindromes need modify how paliproduct function works:
def paliproduct(i1, i2): palindromes = [] while i1 < 1000 , i2 < 1000: product = i1 * i2 i1 += 1 i2 += 1 if product == reverseinteger(product): palindromes.append(product) return palindromes what i've done created palindromes list. every time product palindrome, append list (this return statement was). @ end of loop, return list.
Comments
Post a Comment