MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / proth

Function proth

maths/special_numbers/proth_number.py:10–59  ·  view source on GitHub ↗

:param number: nth number to calculate in the sequence :return: the nth number in Proth number Note: indexing starts at 1 i.e. proth(1) gives the first Proth number of 3 >>> proth(6) 25 >>> proth(0) Traceback (most recent call last): ... ValueError: Input val

(number: int)

Source from the content-addressed store, hash-verified

8
9
10def proth(number: int) -> int:
11 """
12 :param number: nth number to calculate in the sequence
13 :return: the nth number in Proth number
14 Note: indexing starts at 1 i.e. proth(1) gives the first Proth number of 3
15 >>> proth(6)
16 25
17 >>> proth(0)
18 Traceback (most recent call last):
19 ...
20 ValueError: Input value of [number=0] must be > 0
21 >>> proth(-1)
22 Traceback (most recent call last):
23 ...
24 ValueError: Input value of [number=-1] must be > 0
25 >>> proth(6.0)
26 Traceback (most recent call last):
27 ...
28 TypeError: Input value of [number=6.0] must be an integer
29 """
30
31 if not isinstance(number, int):
32 msg = f"Input value of [number={number}] must be an integer"
33 raise TypeError(msg)
34
35 if number < 1:
36 msg = f"Input value of [number={number}] must be > 0"
37 raise ValueError(msg)
38 elif number == 1:
39 return 3
40 elif number == 2:
41 return 5
42 else:
43 """
44 +1 for binary starting at 0 i.e. 2^0, 2^1, etc.
45 +1 to start the sequence at the 3rd Proth number
46 Hence, we have a +2 in the below statement
47 """
48 block_index = int(math.log(number // 3, 2)) + 2
49
50 proth_list = [3, 5]
51 proth_index = 2
52 increment = 3
53 for block in range(1, block_index):
54 for _ in range(increment):
55 proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1])
56 proth_index += 1
57 increment *= 2
58
59 return proth_list[number - 1]
60
61
62def is_proth_number(number: int) -> bool:

Callers 1

proth_number.pyFile · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected