Skip to content

Commit bc5b92f

Browse files
MeIbtihajnaeemcclauss
authored andcommitted
Harmonic Geometric and P-Series Added (TheAlgorithms#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
1 parent d385472 commit bc5b92f

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

maths/series/geometric_series.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
This is a pure Python implementation of the Geometric Series algorithm
3+
https://en.wikipedia.org/wiki/Geometric_series
4+
5+
Run the doctests with the following command:
6+
python3 -m doctest -v geometric_series.py
7+
or
8+
python -m doctest -v geometric_series.py
9+
For manual testing run:
10+
python3 geometric_series.py
11+
"""
12+
13+
14+
def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list:
15+
"""Pure Python implementation of Geometric Series algorithm
16+
:param nth_term: The last term (nth term of Geometric Series)
17+
:param start_term_a : The first term of Geometric Series
18+
:param common_ratio_r : The common ratio between all the terms
19+
:return: The Geometric Series starting from first term a and multiple of common
20+
ration with first term with increase in power till last term (nth term)
21+
Examples:
22+
>>> geometric_series(4, 2, 2)
23+
[2, '4.0', '8.0', '16.0']
24+
>>> geometric_series(4.0, 2.0, 2.0)
25+
[2.0, '4.0', '8.0', '16.0']
26+
>>> geometric_series(4.1, 2.1, 2.1)
27+
[2.1, '4.41', '9.261000000000001', '19.448100000000004']
28+
>>> geometric_series(4, 2, -2)
29+
[2, '-4.0', '8.0', '-16.0']
30+
>>> geometric_series(4, -2, 2)
31+
[-2, '-4.0', '-8.0', '-16.0']
32+
>>> geometric_series(-4, 2, 2)
33+
[]
34+
>>> geometric_series(0, 100, 500)
35+
[]
36+
>>> geometric_series(1, 1, 1)
37+
[1]
38+
>>> geometric_series(0, 0, 0)
39+
[]
40+
"""
41+
if "" in (nth_term, start_term_a, common_ratio_r):
42+
return ""
43+
series = []
44+
power = 1
45+
multiple = common_ratio_r
46+
for _ in range(int(nth_term)):
47+
if series == []:
48+
series.append(start_term_a)
49+
else:
50+
power += 1
51+
series.append(str(float(start_term_a) * float(multiple)))
52+
multiple = pow(float(common_ratio_r), power)
53+
return series
54+
55+
56+
if __name__ == "__main__":
57+
nth_term = input("Enter the last number (n term) of the Geometric Series")
58+
start_term_a = input("Enter the starting term (a) of the Geometric Series")
59+
common_ratio_r = input(
60+
"Enter the common ratio between two terms (r) of the Geometric Series"
61+
)
62+
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
63+
print(geometric_series(nth_term, start_term_a, common_ratio_r))

maths/series/harmonic_series.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
This is a pure Python implementation of the Harmonic Series algorithm
3+
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)
4+
5+
For doctests run following command:
6+
python -m doctest -v harmonic_series.py
7+
or
8+
python3 -m doctest -v harmonic_series.py
9+
10+
For manual testing run:
11+
python3 harmonic_series.py
12+
"""
13+
14+
15+
def 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 n_term
37+
series = []
38+
for temp in range(int(n_term)):
39+
series.append(f"1/{temp + 1}" if series else "1")
40+
return series
41+
42+
43+
if __name__ == "__main__":
44+
nth_term = input("Enter the last number (nth term) of the Harmonic Series")
45+
print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n")
46+
print(harmonic_series(nth_term))

maths/series/p_series.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
This is a pure Python implementation of the P-Series algorithm
3+
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series
4+
5+
For doctests run following command:
6+
python -m doctest -v p_series.py
7+
or
8+
python3 -m doctest -v p_series.py
9+
10+
For manual testing run:
11+
python3 p_series.py
12+
"""
13+
14+
15+
def p_series(nth_term: int, power: int) -> list:
16+
"""Pure Python implementation of P-Series algorithm
17+
18+
:return: The P-Series starting from 1 to last (nth) term
19+
20+
Examples:
21+
>>> p_series(5, 2)
22+
[1, '1/4', '1/9', '1/16', '1/25']
23+
>>> p_series(-5, 2)
24+
[]
25+
>>> p_series(5, -2)
26+
[1, '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04']
27+
>>> p_series("", 1000)
28+
''
29+
>>> p_series(0, 0)
30+
[]
31+
>>> p_series(1, 1)
32+
[1]
33+
"""
34+
if nth_term == "":
35+
return nth_term
36+
nth_term = int(nth_term)
37+
power = int(power)
38+
series = []
39+
for temp in range(int(nth_term)):
40+
series.append(f"1/{pow(temp + 1, int(power))}" if series else 1)
41+
return series
42+
43+
44+
if __name__ == "__main__":
45+
nth_term = input("Enter the last number (nth term) of the P-Series")
46+
power = input("Enter the power for P-Series")
47+
print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p")
48+
print(p_series(nth_term, power))

0 commit comments

Comments
 (0)