Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #A number chain is created by continuously adding the square of the digits in a number to form a new number
- # until it has been seen before.
- #For example,
- #44 > 32 > 13 > 10 > 1 > 1 (44 is called a happy number)
- #85 > 89 > 145 > 42 > 20 > 4 > 16 > 37 > 58 > 89
- #Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop.
- # What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.
- #How many starting numbers below ten million will arrive at 89?
- def isUnHappy(n):
- """ Assumes n an int > 0
- """
- if n == 89:
- return True
- elif n == 1:
- return False
- nStr = str(n)
- digitsSqrSum = 0
- for digit in nStr:
- digitsSqrSum += (int(digit))**2
- return isUnHappy(digitsSqrSum)
- count = 0
- for i in range(1,10000000):
- if isUnHappy(i):
- count += 1
- print count
Advertisement
Add Comment
Please, Sign In to add comment