/ Published in: Python
Favorite's Einstein Puzzle that given a three digit number that the hundred's digit differs from the one's digit by at least two always returns 1089.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# introduction print print('This is a puzzle favored by Einstein. You will be asked to enter\n \ a three digit number, where the hundred\'s digit differs from the\n \ one\'s digit by at least two. The procedure will always yield 1089\n') res = input('Give me a number --> ') # checks if the user enters a valid number while True: if len(res) == 3 and res.isdigit() and abs(int(res[0]) - int(res[2])) >= 2: difference = int(res) - int(res[::-1]) # value used later in calculations break else: res = input('Give me a number --> ') # checks to make a normal difference and not to get negatives if int(res) < int(res[::-1]): difference = int(res[::-1]) - int(res) # show and make calculations print('For the number:', res, 'the reverse number is:', res[::-1]) print('The difference between', res, 'and', res[::-1], 'is', difference) print('The reverse difference is', str(difference)[::-1]) print('The sum of', str(difference), 'and the reverse difference', \ str(difference)[::-1], 'is', str(difference + int(str(difference)[::-1])))