aboutsummaryrefslogtreecommitdiff
path: root/challenge-008/lubos-kolouch/python/ch-1.py
blob: 510cecef94e3ddc7253d5fa3c37daa43b6c61c1b (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def is_prime(num: int) -> bool:
    if num < 2:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True


def get_next_prime(num: int) -> int:
    num += 1
    while not is_prime(num):
        num += 1
    return num


perfect_numbers = []
p = 2
num = 2 ** (p - 1) * (2**p - 1)

while len(perfect_numbers) < 5:
    perfect_numbers.append(num)
    p = get_next_prime(p)
    num = 2 ** (p - 1) * (2**p - 1)

print("The first five perfect numbers are:", perfect_numbers)