Posted By


a7xrturo on 11/16/13

Tagged


Statistics


Viewed 449 times
Favorited by 0 user(s)

Favorite's Einstein Puzzle


/ Published in: Python
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. # introduction print
  2.  
  3. print('This is a puzzle favored by Einstein. You will be asked to enter\n \
  4. a three digit number, where the hundred\'s digit differs from the\n \
  5. one\'s digit by at least two. The procedure will always yield 1089\n')
  6.  
  7. res = input('Give me a number --> ')
  8.  
  9. # checks if the user enters a valid number
  10. while True:
  11. if len(res) == 3 and res.isdigit() and abs(int(res[0]) - int(res[2])) >= 2:
  12. difference = int(res) - int(res[::-1]) # value used later in calculations
  13. break
  14. else:
  15. res = input('Give me a number --> ')
  16.  
  17. # checks to make a normal difference and not to get negatives
  18. if int(res) < int(res[::-1]):
  19. difference = int(res[::-1]) - int(res)
  20.  
  21. # show and make calculations
  22. print('For the number:', res, 'the reverse number is:', res[::-1])
  23.  
  24. print('The difference between', res, 'and', res[::-1], 'is', difference)
  25.  
  26. print('The reverse difference is', str(difference)[::-1])
  27.  
  28. print('The sum of', str(difference), 'and the reverse difference', \
  29. str(difference)[::-1], 'is', str(difference + int(str(difference)[::-1])))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.