forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
d385472
commit bc5b92f
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
This is a pure Python implementation of the Geometric Series algorithm | ||
https://en.wikipedia.org/wiki/Geometric_series | ||
Run the doctests with the following command: | ||
python3 -m doctest -v geometric_series.py | ||
or | ||
python -m doctest -v geometric_series.py | ||
For manual testing run: | ||
python3 geometric_series.py | ||
""" | ||
|
||
|
||
def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list: | ||
"""Pure Python implementation of Geometric Series algorithm | ||
:param nth_term: The last term (nth term of Geometric Series) | ||
:param start_term_a : The first term of Geometric Series | ||
:param common_ratio_r : The common ratio between all the terms | ||
:return: The Geometric Series starting from first term a and multiple of common | ||
ration with first term with increase in power till last term (nth term) | ||
Examples: | ||
>>> geometric_series(4, 2, 2) | ||
[2, '4.0', '8.0', '16.0'] | ||
>>> geometric_series(4.0, 2.0, 2.0) | ||
[2.0, '4.0', '8.0', '16.0'] | ||
>>> geometric_series(4.1, 2.1, 2.1) | ||
[2.1, '4.41', '9.261000000000001', '19.448100000000004'] | ||
>>> geometric_series(4, 2, -2) | ||
[2, '-4.0', '8.0', '-16.0'] | ||
>>> geometric_series(4, -2, 2) | ||
[-2, '-4.0', '-8.0', '-16.0'] | ||
>>> geometric_series(-4, 2, 2) | ||
[] | ||
>>> geometric_series(0, 100, 500) | ||
[] | ||
>>> geometric_series(1, 1, 1) | ||
[1] | ||
>>> geometric_series(0, 0, 0) | ||
[] | ||
""" | ||
if "" in (nth_term, start_term_a, common_ratio_r): | ||
return "" | ||
series = [] | ||
power = 1 | ||
multiple = common_ratio_r | ||
for _ in range(int(nth_term)): | ||
if series == []: | ||
series.append(start_term_a) | ||
else: | ||
power += 1 | ||
series.append(str(float(start_term_a) * float(multiple))) | ||
multiple = pow(float(common_ratio_r), power) | ||
return series | ||
|
||
|
||
if __name__ == "__main__": | ||
nth_term = input("Enter the last number (n term) of the Geometric Series") | ||
start_term_a = input("Enter the starting term (a) of the Geometric Series") | ||
common_ratio_r = input( | ||
"Enter the common ratio between two terms (r) of the Geometric Series" | ||
) | ||
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") | ||
print(geometric_series(nth_term, start_term_a, common_ratio_r)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |