Compare commits

...

8 Commits

Author SHA1 Message Date
Arijit Kumar Das
8466f5efd8
Merge 6777f1ebd3 into f3f32ae3ca 2024-11-21 21:34:47 +05:30
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
pre-commit-ci[bot]
6777f1ebd3 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-11-01 14:38:05 +00:00
Arijit Kumar Das
3f47a1f5b9
Update logarithmic_series.py 2024-11-01 19:49:51 +05:30
pre-commit-ci[bot]
273ee447bf [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-23 19:44:16 +00:00
Arijit Kumar Das
25c5430b47
Update logarithmic_series.py
Fixed long line issue
2024-10-24 01:13:40 +05:30
pre-commit-ci[bot]
469f0fc4ec [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-23 19:36:44 +00:00
Arijit Kumar Das
1f03223571
Create logarithmic_series.py
This is an implementation of logarithmic series in Python.
Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series
2024-10-24 01:00:15 +05:30
2 changed files with 55 additions and 1 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus - id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3 rev: v0.7.4
hooks: hooks:
- id: ruff - id: ruff
- id: ruff-format - id: ruff-format

View File

@ -0,0 +1,54 @@
"""
This is an implementation of logarithmic series in Python.
Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series
"""
def logarithmic_series(x_value: float, n_terms: int = 5, expand: bool = False) -> list:
"""
Returns the logarithmic series for a number x (log x) upto n terms.
Parameters:
x_value: a floating point number for log(x)
n_terms: number of terms to be computed
expand: Set this flag to get the terms as real numbers,
unset for unsolved expressions
Examples:
>>> logarithmic_series(3)
['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5']
>>> logarithmic_series(-3)
['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5']
>>> logarithmic_series(3, 6)
['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6']
>>> logarithmic_series(3, expand=True)
[2.0, -2.0, 2.6666666666666665, -4.0, 6.4]
"""
n_times_x_minus_1: float = x_value - 1
n: int = 1
series: list = []
for _ in range(n_terms):
if expand:
series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n))
n_times_x_minus_1 *= x_value - 1
else:
sign: str = "-" if (-1) ** (n + 1) == -1 else ""
term: str = (
sign + "(" + str(x_value - 1) + "^" + str(n) + ")" + "/" + str(n)
)
if term.startswith("-(-"):
term = "(" + term[3::]
elif term.startswith("(-"):
term = "-(" + term[2::]
series.append(term)
n += 1
return series
if __name__ == "__main__":
import doctest
doctest.testmod()