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

Function harmonic_series

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

Pure Python implementation of Harmonic Series algorithm :param n_term: The last (nth) term of Harmonic Series :return: The Harmonic Series starting from 1 to last (nth) term Examples: >>> harmonic_series(5) ['1', '1/2', '1/3', '1/4', '1/5'] >>> harmonic_series(5.0) ['1'

(n_term: str)

Source from the content-addressed store, hash-verified

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

Callers 1

harmonic_series.pyFile · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected