Skip to content

Commit 22d2453

Browse files
AugustofCravoharshildarji
authored andcommitted
Create Quadratic Equations(Complexes Numbers) (TheAlgorithms#941)
* Create Quadratic Equations(Complexes Numbers) Created function that solves quadratic equations treating the cases with complexes numbers. Giving an answer with the imaginary unit "i". * Update Quadratic Equations(Complexes Numbers) Since there was no response from the owner of this PR, I made this little change which I hope will solve the issue!
1 parent 47bc34a commit 22d2453

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import print_function
2+
import math
3+
4+
def QuadraticEquation(a,b,c):
5+
"""
6+
Prints the solutions for a quadratic equation, given the numerical coefficients a, b and c,
7+
for a*x*x + b*x + c.
8+
Ex.: a = 1, b = 3, c = -4
9+
Solution1 = 1 and Solution2 = -4
10+
"""
11+
Delta = b*b - 4*a*c
12+
if a != 0:
13+
if Delta >= 0:
14+
Solution1 = (-b + math.sqrt(Delta))/(2*a)
15+
Solution2 = (-b - math.sqrt(Delta))/(2*a)
16+
print ("The equation solutions are: ", Solution1," and ", Solution2)
17+
else:
18+
"""
19+
Treats cases of Complexes Solutions(i = imaginary unit)
20+
Ex.: a = 5, b = 2, c = 1
21+
Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10
22+
"""
23+
if b > 0:
24+
print("The equation solutions are: (-",b,"+",math.sqrt(-Delta),"*i)/2 and (-",b,"+",math.sqrt(-Delta),"*i)/", 2*a)
25+
if b < 0:
26+
print("The equation solutions are: (",b,"+",math.sqrt(-Delta),"*i)/2 and (",b,"+",math.sqrt(-Delta),"*i/",2*a)
27+
if b == 0:
28+
print("The equation solutions are: (",math.sqrt(-Delta),"*i)/2 and ",math.sqrt(-Delta),"*i)/", 2*a)
29+
else:
30+
print("Error. Please, coeficient 'a' must not be zero for quadratic equations.")
31+
def main():
32+
a = 5
33+
b = 6
34+
c = 1
35+
36+
QuadraticEquation(a,b,c) # The equation solutions are: -0.2 and -1.0
37+
38+
39+
if __name__ == '__main__':
40+
main()

0 commit comments

Comments
 (0)