-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhappiness.py
More file actions
39 lines (29 loc) · 811 Bytes
/
happiness.py
File metadata and controls
39 lines (29 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
# Southeast USA 2012 6222
def happify(num):
sum_of_squares = 0
for c in str(num):
sum_of_squares += int(c) ** 2
return sum_of_squares
def happiness_distance(num):
used_list = []
iterations = 0
while(True):
if num in used_list:
return -1 # num is unhappy
used_list.append(num)
if num == 1:
return iterations
num = happify(num)
iterations += 1
def count_unhappies(lower, upper):
unhappies = 0
for i in range(lower, upper + 1):
if happiness_distance(i) == -1:
unhappies += 1
return unhappies
while True:
lower, upper = map(int, input().split(' '))
if lower == 0 and upper == 0:
break
print(count_unhappies(lower, upper))