-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadratic_equations.py
33 lines (28 loc) · 1022 Bytes
/
quadratic_equations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def formulas(a, b, c):
"""
Nature of roots:
D > 0, roots are real and distinct (unequal)
D = 0, roots are real and equal (coincident)
D < 0, roots are imaginary and unequal
"""
a, b, c = int(a), int(b), int(c)
if a > 0:
# Calculate discriminant
d = (b**2) - 4*a*c
val_sqrt = d**(1/2)
# Check Condition discriminant
if d > 0:
formula_plus = (-b + val_sqrt) / (2 * a)
formula_min = (-b - val_sqrt) / (2 * a)
print('roots are real and distinct (unequal)')
print(f'+ result = {formula_plus}')
print(f'- result = {formula_min}')
elif d == 0:
print('roots are real and equal (coincident)')
print(f'result = {-b /(2 * a)}')
else:
print('roots are imaginary and unequal')
print(- b / (2 * a), " + i", val_sqrt)
print(- b / (2 * a), " - i", val_sqrt)
else:
raise ValueError('VALUE A MUST BE > 0')