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

Function sylvester

maths/sylvester_sequence.py:11–40  ·  view source on GitHub ↗

:param number: nth number to calculate in the sequence :return: the nth number in Sylvester's sequence >>> sylvester(8) 113423713055421844361000443 >>> sylvester(-1) Traceback (most recent call last): ... ValueError: The input value of [n=-1] has to be > 0

(number: int)

Source from the content-addressed store, hash-verified

9
10
11def sylvester(number: int) -> int:
12 """
13 :param number: nth number to calculate in the sequence
14 :return: the nth number in Sylvester's sequence
15
16 >>> sylvester(8)
17 113423713055421844361000443
18
19 >>> sylvester(-1)
20 Traceback (most recent call last):
21 ...
22 ValueError: The input value of [n=-1] has to be > 0
23
24 >>> sylvester(8.0)
25 Traceback (most recent call last):
26 ...
27 AssertionError: The input value of [n=8.0] is not an integer
28 """
29 assert isinstance(number, int), f"The input value of [n={number}] is not an integer"
30
31 if number == 1:
32 return 2
33 elif number < 1:
34 msg = f"The input value of [n={number}] has to be > 0"
35 raise ValueError(msg)
36 else:
37 num = sylvester(number - 1)
38 lower = num - 1
39 upper = num
40 return lower * upper + 1
41
42
43if __name__ == "__main__":

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected