mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
61f3119467
* f-string update rsa_cipher.py * f-string update rsa_key_generator.py * f-string update burrows_wheeler.py * f-string update non_recursive_segment_tree.py * f-string update red_black_tree.py * f-string update deque_doubly.py * f-string update climbing_stairs.py * f-string update iterating_through_submasks.py * f-string update knn_sklearn.py * f-string update 3n_plus_1.py * f-string update quadratic_equations_complex_numbers.py * f-string update nth_fibonacci_using_matrix_exponentiation.py * f-string update sherman_morrison.py * f-string update levenshtein_distance.py * fix lines that were too long
39 lines
929 B
Python
39 lines
929 B
Python
from __future__ import annotations
|
|
|
|
from cmath import sqrt
|
|
|
|
|
|
def quadratic_roots(a: int, b: int, c: int) -> tuple[complex, complex]:
|
|
"""
|
|
Given the numerical coefficients a, b and c,
|
|
calculates the roots for any quadratic equation of the form ax^2 + bx + c
|
|
|
|
>>> quadratic_roots(a=1, b=3, c=-4)
|
|
(1.0, -4.0)
|
|
>>> quadratic_roots(5, 6, 1)
|
|
(-0.2, -1.0)
|
|
>>> quadratic_roots(1, -6, 25)
|
|
((3+4j), (3-4j))
|
|
"""
|
|
|
|
if a == 0:
|
|
raise ValueError("Coefficient 'a' must not be zero.")
|
|
delta = b * b - 4 * a * c
|
|
|
|
root_1 = (-b + sqrt(delta)) / (2 * a)
|
|
root_2 = (-b - sqrt(delta)) / (2 * a)
|
|
|
|
return (
|
|
root_1.real if not root_1.imag else root_1,
|
|
root_2.real if not root_2.imag else root_2,
|
|
)
|
|
|
|
|
|
def main():
|
|
solution1, solution2 = quadratic_roots(a=5, b=6, c=1)
|
|
print(f"The solutions are: {solution1} and {solution2}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|