mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Harmonic Geometric and P-Series Added (#1633)
* Harmonic Geometric and P-Series Added * Editing comments * Update and rename series/Geometric_Series.py to maths/series/geometric_series.py * Update and rename series/Harmonic_Series.py to maths/series/harmonic_series.py * Update and rename series/P_Series.py to maths/series/p_series.py
This commit is contained in:

committed by
Christian Clauss

parent
d385472c6f
commit
bc5b92f7f9
46
maths/series/harmonic_series.py
Normal file
46
maths/series/harmonic_series.py
Normal file
@ -0,0 +1,46 @@
|
||||
"""
|
||||
This is a pure Python implementation of the Harmonic Series algorithm
|
||||
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)
|
||||
|
||||
For doctests run following command:
|
||||
python -m doctest -v harmonic_series.py
|
||||
or
|
||||
python3 -m doctest -v harmonic_series.py
|
||||
|
||||
For manual testing run:
|
||||
python3 harmonic_series.py
|
||||
"""
|
||||
|
||||
|
||||
def harmonic_series(n_term: str) -> list:
|
||||
"""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', '1/2', '1/3', '1/4', '1/5']
|
||||
>>> harmonic_series(5.1)
|
||||
['1', '1/2', '1/3', '1/4', '1/5']
|
||||
>>> harmonic_series(-5)
|
||||
[]
|
||||
>>> harmonic_series(0)
|
||||
[]
|
||||
>>> harmonic_series(1)
|
||||
['1']
|
||||
"""
|
||||
if n_term == "":
|
||||
return n_term
|
||||
series = []
|
||||
for temp in range(int(n_term)):
|
||||
series.append(f"1/{temp + 1}" if series else "1")
|
||||
return series
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
nth_term = input("Enter the last number (nth term) of the Harmonic Series")
|
||||
print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n")
|
||||
print(harmonic_series(nth_term))
|
Reference in New Issue
Block a user