mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
1f03223571
commit
469f0fc4ec
|
@ -3,6 +3,7 @@ This is an implementation of logarithmic series in Python.
|
||||||
Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series
|
Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list:
|
def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list:
|
||||||
"""
|
"""
|
||||||
Returns the logarithmic series for a number x (log x) upto n terms.
|
Returns the logarithmic series for a number x (log x) upto n terms.
|
||||||
|
@ -29,20 +30,22 @@ def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list
|
||||||
n: int = 1
|
n: int = 1
|
||||||
series: list = []
|
series: list = []
|
||||||
for _ in range(n_terms):
|
for _ in range(n_terms):
|
||||||
if (expand):
|
if expand:
|
||||||
series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n))
|
series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n))
|
||||||
n_times_x_minus_1 *= (x-1)
|
n_times_x_minus_1 *= x - 1
|
||||||
else:
|
else:
|
||||||
sign: str = '-' if (-1)**(n+1) == -1 else ''
|
sign: str = "-" if (-1) ** (n + 1) == -1 else ""
|
||||||
term: str = sign+'('+str(x-1)+'^'+str(n)+')'+'/'+str(n)
|
term: str = sign + "(" + str(x - 1) + "^" + str(n) + ")" + "/" + str(n)
|
||||||
if (term.startswith("-(-")):
|
if term.startswith("-(-"):
|
||||||
term = '('+term[3::]
|
term = "(" + term[3::]
|
||||||
elif (term.startswith("(-")):
|
elif term.startswith("(-"):
|
||||||
term = "-(" + term[2::]
|
term = "-(" + term[2::]
|
||||||
series.append(term)
|
series.append(term)
|
||||||
n += 1
|
n += 1
|
||||||
return series
|
return series
|
||||||
|
|
||||||
if (__name__ == "__main__"):
|
|
||||||
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user