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

Function p_series

maths/series/p_series.py:15–40  ·  view source on GitHub ↗

Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term Examples: >>> p_series(5, 2) ['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25'] >>> p_series(-5, 2) [] >>> p_series(5, -2) ['1', '1 / 0.25', '1 / 0.1111111111111111

(nth_term: float | str, power: float | str)

Source from the content-addressed store, hash-verified

13
14
15def p_series(nth_term: float | str, power: float | str) -> list[str]:
16 """
17 Pure Python implementation of P-Series algorithm
18 :return: The P-Series starting from 1 to last (nth) term
19 Examples:
20 >>> p_series(5, 2)
21 ['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25']
22 >>> p_series(-5, 2)
23 []
24 >>> p_series(5, -2)
25 ['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04']
26 >>> p_series("", 1000)
27 ['']
28 >>> p_series(0, 0)
29 []
30 >>> p_series(1, 1)
31 ['1']
32 """
33 if nth_term == "":
34 return [""]
35 nth_term = int(nth_term)
36 power = int(power)
37 series: list[str] = []
38 for temp in range(int(nth_term)):
39 series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1")
40 return series
41
42
43if __name__ == "__main__":

Callers 1

p_series.pyFile · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected