mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Add Sylvester's sequence to maths (#5171)
* Add Sylvester's sequence to maths * Update sylvester_sequence.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
97562c19f8
commit
729b4d875a
43
maths/sylvester_sequence.py
Normal file
43
maths/sylvester_sequence.py
Normal file
@ -0,0 +1,43 @@
|
||||
"""
|
||||
|
||||
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):
|
||||
...
|
||||
ValueError: The input value of [n=-1] has to be > 0
|
||||
|
||||
>>> sylvester(8.0)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
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:
|
||||
raise ValueError(f"The input value of [n={number}] has to be > 0")
|
||||
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)}")
|
Loading…
Reference in New Issue
Block a user