2021-10-10 18:02:44 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Calculates the nth number in Sylvester's sequence
|
|
|
|
|
|
|
|
Source:
|
|
|
|
https://en.wikipedia.org/wiki/Sylvester%27s_sequence
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def sylvester(number: int) -> int:
|
|
|
|
"""
|
|
|
|
: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):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2021-10-10 18:02:44 +00:00
|
|
|
ValueError: The input value of [n=-1] has to be > 0
|
|
|
|
|
|
|
|
>>> sylvester(8.0)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2021-10-10 18:02:44 +00:00
|
|
|
AssertionError: The input value of [n=8.0] is not an integer
|
|
|
|
"""
|
|
|
|
assert isinstance(number, int), f"The input value of [n={number}] is not an integer"
|
|
|
|
|
|
|
|
if number == 1:
|
|
|
|
return 2
|
|
|
|
elif number < 1:
|
2023-05-26 07:34:17 +00:00
|
|
|
msg = f"The input value of [n={number}] has to be > 0"
|
|
|
|
raise ValueError(msg)
|
2021-10-10 18:02:44 +00:00
|
|
|
else:
|
|
|
|
num = sylvester(number - 1)
|
|
|
|
lower = num - 1
|
|
|
|
upper = num
|
|
|
|
return lower * upper + 1
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(f"The 8th number in Sylvester's sequence: {sylvester(8)}")
|