Compare commits

...

13 Commits

Author SHA1 Message Date
Nanoemm
5930fc906a
Merge 33266f262f5c45fd9adf9ad3b7d02fb378d0b05b into 338cbafe0d5b07d57f83060ea0f9ba3a6c1155e7 2025-02-10 01:45:36 +00:00
lighting9999
338cbafe0d
Improve power.py (#12567)
* Fix And Add power.py

To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-09 20:51:18 +03:00
Pablito
33266f262f Minor changes to type hints 2024-12-04 14:51:02 +01:00
Pablito
555b54cbbc Minor changes to type hints 2024-12-04 14:49:54 +01:00
Pablito
60ad958472 Added type hints for tests 2024-12-04 14:46:18 +01:00
Pablito
20c3200a62 Minor changes to some lists in the tests 2024-12-04 14:44:57 +01:00
Pablito
dddc326d0d Returned to previous commit with a variable name changed 2024-12-04 14:42:13 +01:00
Pablito
02f7cfac25 Added type hints and changed to descriptive names 2024-12-04 14:37:20 +01:00
Pablito
a3a71d2ef1 Removed an unused function 2024-12-03 11:27:22 +01:00
Pablito
be2ab89982 Changed the import block 2024-12-03 11:26:06 +01:00
pre-commit-ci[bot]
bb1d79fed8 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-12-03 10:22:48 +00:00
Nanoemm
08d1f15d1b updating DIRECTORY.md 2024-12-03 10:19:40 +00:00
Pablito
7036c84cb5 Implemented Legendre polynomial computation algorithm
Added an algorithm that calculates the coefficients of the Legendre polynomial of degree n using the recurrence relation
2024-12-03 11:19:20 +01:00
3 changed files with 63 additions and 5 deletions

View File

@ -726,6 +726,7 @@
* [Pollard Rho](maths/pollard_rho.py)
* [Polynomial Evaluation](maths/polynomial_evaluation.py)
* Polynomials
* [Legendre](maths/polynomials/legendre.py)
* [Single Indeterminate Operations](maths/polynomials/single_indeterminate_operations.py)
* [Power Using Recursion](maths/power_using_recursion.py)
* [Prime Check](maths/prime_check.py)

View File

@ -1,4 +1,4 @@
def actual_power(a: int, b: int):
def actual_power(a: int, b: int) -> int:
"""
Function using divide and conquer to calculate a^b.
It only works for integer a,b.
@ -19,10 +19,12 @@ def actual_power(a: int, b: int):
"""
if b == 0:
return 1
half = actual_power(a, b // 2)
if (b % 2) == 0:
return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
return half * half
else:
return a * actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
return a * half * half
def power(a: int, b: int) -> float:
@ -43,9 +45,9 @@ def power(a: int, b: int) -> float:
-0.125
"""
if b < 0:
return 1 / actual_power(a, b)
return 1 / actual_power(a, -b)
return actual_power(a, b)
if __name__ == "__main__":
print(power(-2, -3))
print(power(-2, -3)) # output -0.125

View File

@ -0,0 +1,55 @@
# Imports de bibliothèques standard
from math import factorial
# Imports de bibliothèques tierces
import pytest
from numpy.polynomial import Polynomial
def legendre(n: int) -> list[float]:
"""
Compute the coefficients of the nth Legendre polynomial.
The Legendre polynomials are solutions to Legendre's differential equation
and are widely used in physics and engineering.
Parameters:
n (int): The order of the Legendre polynomial.
Returns:
list[float]: Coefficients of the polynomial in ascending order of powers.
"""
legendre_polynomial = (1 / (factorial(n) * (2**n))) * (Polynomial([-1, 0, 1]) ** n)
return legendre_polynomial.deriv(n).coef.tolist()
def test_legendre_0() -> None:
"""Test the 0th Legendre polynomial."""
assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]"
def test_legendre_1() -> None:
"""Test the 1st Legendre polynomial."""
assert legendre(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]"
def test_legendre_2() -> None:
"""Test the 2nd Legendre polynomial."""
assert legendre(2) == [-0.5, 0.0, 1.5]
"The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]"
def test_legendre_3() -> None:
"""Test the 3rd Legendre polynomial."""
assert legendre(3) == [0.0, -1.5, 0.0, 2.5]
"The 3rd Legendre polynomial should be [0.0, -1.5, 0.0, 2.5]"
def test_legendre_4() -> None:
"""Test the 4th Legendre polynomial."""
assert legendre(4) == pytest.approx([0.375, 0.0, -3.75, 0.0, 4.375])
"The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]"
if __name__ == "__main__":
pytest.main()