Skip to content

Commit

Permalink
Happy Numbers done
Browse files Browse the repository at this point in the history
  • Loading branch information
Karan Goel committed Jul 26, 2013
1 parent b9b6fd1 commit 1f7ee1e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Numbers/happy_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Happy Numbers - A happy number is defined by the
following process. Starting with any positive integer,
replace the number by the sum of the squares of its
digits, and repeat the process until the number equals
1 (where it will stay), or it loops endlessly in a
cycle which does not include 1. Those numbers for which
this process ends in 1 are happy numbers, while those
that do not end in 1 are unhappy numbers. Take an input
number from user, and find first 8 happy numbers from
that input.
"""

NUMBERS_REQUIRED = 8 # number of happy numbers required

def is_happy_number(num):
seen = []
while True:
sum_digits = sum(int(digit) ** 2 for digit in str(num))
if sum_digits == 1:
return True
elif sum_digits in seen:
return False
else:
num = sum_digits
seen.append(num)

if __name__ == '__main__':

happies = [] # list of happy numbers found

num = input('Start at: ')

while len(happies) != NUMBERS_REQUIRED:
if is_happy_number(num):
happies.append(num)
num += 1

print happies

0 comments on commit 1f7ee1e

Please sign in to comment.