-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappy_num.py
43 lines (31 loc) · 970 Bytes
/
happy_num.py
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
40
41
42
43
from math import pow
from typing import List, Iterator
def is_first_iteration(n: int) -> bool:
prev: str = "0"
for (i, curr) in enumerate(str(n)):
if i > 0:
if prev > curr:
return False
prev = curr
return True
def square_sum(n: int) -> int:
ss: float = 0
val: int = 0 + n
while val > 0:
ss += pow((val % 10), 2)
val = int(val / 10)
return int(ss)
def is_happy(n: int) -> bool:
unhappy_markers: List[int] = [89, 145, 42, 37, 58, 20, 4, 16]
ss: int = n
while True:
ss = int(square_sum(ss))
if ss in unhappy_markers: # definitely not happy
return False
elif ss == 1: # therefore happy
return True
def get_dist_happy(r: int) -> int:
count: int = 0
happy_nums: Iterator[int] = (1 for i in range(1, r + 1)
if is_first_iteration(i) & is_happy(i))
return sum(happy_nums)