-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathp_series.py
38 lines (33 loc) · 935 Bytes
/
p_series.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
"""
This is a pure Python implementation of the P-Series algorithm
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series
For doctests run following command:
python -m doctest -v p_series.py
or
python3 -m doctest -v p_series.py
For manual testing run:
python3 p_series.py
"""
def p_series(nth_term: int, power: int) -> list[float]:
"""
Pure Python implementation of P-Series algorithm
:return: The P-Series starting from 1 to last (nth) term
Examples:
>>> p_series(5, 2)
[1.0, 0.25, 0.1111111111111111, 0.0625, 0.04]
>>> p_series(-5, 2)
[]
>>> p_series(5, -2)
[1.0, 4.0, 9.0, 16.0, 25.0]
>>> p_series(0, 0)
[]
>>> p_series(1, 1)
[1.0]
"""
series: list[float] = []
for temp in range(nth_term):
series.append(1 / (pow(temp + 1, power)) if series else 1.0)
return series
if __name__ == "__main__":
import doctest
doctest.testmod()