forked from karan/Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Karan Goel
committed
Jul 26, 2013
1 parent
b9b6fd1
commit 1f7ee1e
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|