-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathproth_number.py
58 lines (45 loc) · 1.33 KB
/
proth_number.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
48
49
50
51
52
53
54
55
56
57
58
# kalkulasi nth angka proth
# https://handwiki.org/wiki/Proth_number
import math
def proth(angka: int) -> int:
"""
>>> proth(-3)
Traceback (most recent call last):
...
ValueError: angka tidak boleh kurang dari 1
>>> proth(6.0)
Traceback (most recent call last):
...
TypeError: Angka harus integer
"""
if not isinstance(angka, int):
raise TypeError("Angka harus integer")
if angka < 1:
raise ValueError("angka tidak boleh kurang dari 1")
elif angka == 1:
return 3
elif angka == 2:
return 5
else:
# +1 untuk biner yang dimulai pada 0 contoh 2 ^0, 2 ^1 dan lainnya
block_index = int(math.log(angka // 3, 2)) + 2
proth_list = [3, 5]
proth_index = 2
increment = 3
for block in range(1, block_index):
for _ in range(increment):
proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1])
proth_index += 1
increment *= 2
return proth_list[angka - 1]
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
for angka in range(11):
value = 0
try:
value = proth(angka)
except ValueError:
print(f"{angka}")
continue
print(f"{angka}, angka proth: {value}")