aboutsummaryrefslogtreecommitdiff
path: root/challenge-164/lubos-kolouch/python/ch-2.py
blob: e64f1c22794ac97a20d5366fdb389b73f07c94b0 (plain)
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
""" Challenge 164 Task 2"""


def is_happy(what: int) -> bool:
    """Check if a number is happy"""

    results_cache: dict = {}

    while 1:
        result = 0
        for num in str(what):
            result += int(num) * int(num)

        if result == 1:
            return True

        if results_cache.get(result, 0):
            return False

        results_cache[result] = 1
        what = result


def get_happy_numbers() -> list:
    """Get the 8 happy numbers"""

    output: list = []

    pos = 1
    while len(output) < 8:
        if is_happy(pos):
            output.append(pos)
        pos += 1

    return output


assert get_happy_numbers() == [1, 7, 10, 13, 19, 23, 28, 31]