loading
main.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
44
45
46
47
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from functools import lru_cache


@lru_cache(maxsize=100000)
def fibonacci(n: int) -> int:
    if 0 <= n <= 2:
        return 1
    elif n > 2:
        return fibonacci(n-1) + fibonacci(n-2)


def is_fibonacci(i):
    history = 0, 1
    while sum(history) < i:
        history = history[1], sum(history)
    return sum(history) == i


def run():
    for i, j in enumerate(range(1, 11)):
        print(f"{i+1:2}: {fibonacci(j):3}")

    # List comprehensions
    lc = [fibonacci(x) for x in range(1, 11)]
    print(lc)

    # set comprehensions
    sc = {(x, fibonacci(x)) for x in range(1, 11)}
    print(sc)

    # dict comprehensions
    dc = {x: fibonacci(x) for x in range(1, 11)}
    print(dc)

    # generator
    gc = (fibonacci(x) for x in range(1, 11))
    print(gc)
    print(tuple(gc))



if __name__ == '__main__':
    run()