55.py 706 B

123456789101112131415161718192021222324252627282930313233
  1. def isPalindrome(n):
  2. """Returns a boolean indicating whether a number is a palindrome"""
  3. n = str(n)
  4. return n == n[::-1]
  5. def reverseAdd(n):
  6. """Take number and add reverse"""
  7. return n + int(str(n)[::-1])
  8. def isLychrel(n):
  9. """Returns a boolean indicating if the number is a Lychrel number"""
  10. iterations = 0
  11. n = reverseAdd(n)
  12. # Maximum of 50 iterations
  13. while iterations < 50:
  14. if isPalindrome(n):
  15. return False
  16. else:
  17. iterations += 1
  18. n = reverseAdd(n)
  19. return True
  20. def main(limit):
  21. """Iterates through all numbers up to limit to find Lychrel numbers"""
  22. numberLychrels = 0
  23. for i in range(1, limit):
  24. if isLychrel(i):
  25. numberLychrels += 1
  26. return numberLychrels
  27. print(main(10000))