2019-12-14 05:46:02 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2021-11-07 15:13:58 +00:00
|
|
|
from __future__ import annotations
|
2019-12-14 05:46:02 +00:00
|
|
|
|
|
|
|
|
2023-08-09 07:55:30 +00:00
|
|
|
def p_series(nth_term: float | str, power: float | str) -> list[str]:
|
2021-11-07 15:13:58 +00:00
|
|
|
"""
|
|
|
|
Pure Python implementation of P-Series algorithm
|
|
|
|
:return: The P-Series starting from 1 to last (nth) term
|
2019-12-14 05:46:02 +00:00
|
|
|
Examples:
|
|
|
|
>>> p_series(5, 2)
|
2021-11-07 15:13:58 +00:00
|
|
|
['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25']
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> p_series(-5, 2)
|
|
|
|
[]
|
|
|
|
>>> p_series(5, -2)
|
2021-11-07 15:13:58 +00:00
|
|
|
['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04']
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> p_series("", 1000)
|
2021-11-07 15:13:58 +00:00
|
|
|
['']
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> p_series(0, 0)
|
|
|
|
[]
|
|
|
|
>>> p_series(1, 1)
|
2021-11-07 15:13:58 +00:00
|
|
|
['1']
|
2019-12-14 05:46:02 +00:00
|
|
|
"""
|
|
|
|
if nth_term == "":
|
2021-11-07 15:13:58 +00:00
|
|
|
return [""]
|
2019-12-14 05:46:02 +00:00
|
|
|
nth_term = int(nth_term)
|
|
|
|
power = int(power)
|
2021-11-07 15:13:58 +00:00
|
|
|
series: list[str] = []
|
2019-12-14 05:46:02 +00:00
|
|
|
for temp in range(int(nth_term)):
|
2021-11-07 15:13:58 +00:00
|
|
|
series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1")
|
2019-12-14 05:46:02 +00:00
|
|
|
return series
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-11-07 15:13:58 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
|
|
|
|
|
|
|
nth_term = int(input("Enter the last number (nth term) of the P-Series"))
|
|
|
|
power = int(input("Enter the power for P-Series"))
|
2019-12-14 05:46:02 +00:00
|
|
|
print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p")
|
|
|
|
print(p_series(nth_term, power))
|