mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +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
48
maths/series/p_series.py
Normal file
48
maths/series/p_series.py
Normal file
@ -0,0 +1,48 @@
|
||||
"""
|
||||
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:
|
||||
"""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', '1/0.0625', '1/0.04']
|
||||
>>> p_series("", 1000)
|
||||
''
|
||||
>>> p_series(0, 0)
|
||||
[]
|
||||
>>> p_series(1, 1)
|
||||
[1]
|
||||
"""
|
||||
if nth_term == "":
|
||||
return nth_term
|
||||
nth_term = int(nth_term)
|
||||
power = int(power)
|
||||
series = []
|
||||
for temp in range(int(nth_term)):
|
||||
series.append(f"1/{pow(temp + 1, int(power))}" if series else 1)
|
||||
return series
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
nth_term = input("Enter the last number (nth term) of the P-Series")
|
||||
power = input("Enter the power for P-Series")
|
||||
print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p")
|
||||
print(p_series(nth_term, power))
|
Reference in New Issue
Block a user