forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
roots_of_quadratic_equation.py
59 lines (49 loc) · 1.51 KB
/
roots_of_quadratic_equation.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
''''
This is the simple python code for finding the roots of quadratic equation.
Approach : Enter the values of a,b,c of the quadratic equation of the form (ax^2 bx + c )
the function quadraticRoots will calculate the Discriminant , if D is greater than 0 then it will find the roots and print them otherwise it will print Imaginary!!
'''
import math
class Solution:
def quadraticRoots(self, a, b, c):
d = ((b*b) - (4*a*c))
if d<0:
lst=[]
lst.append(-1)
return lst
D = int(math.sqrt(d))
x1 = math.floor((-1*b + D)/(2*a))
x2 = math.floor((-1*b - D)/(2*a))
lst = []
lst.append(int(x1))
lst.append(int(x2))
lst.sort()
lst.reverse()
return lst
# Driver Code Starts
if __name__ == '__main__':
# For the values of a,b,c taking in a list in one line eg : 1 2 3
print("Enter the values for a,b,c of the equation of the form ax^2 + bx +c")
abc=[int(x) for x in input().strip().split()]
a=abc[0]
b=abc[1]
c=abc[2]
# Making a object to class Solution
ob = Solution()
ans = ob.quadraticRoots(a,b,c)
if len(ans)==1 and ans[0]==-1:
print("Imaginary Roots")
else:
print("Roots are :",end=" ")
for i in range(len(ans)):
print(ans[i], end=" ")
print()
'''
Sample Input/Output:
Enter the values for a,b,c of the equation of the form ax^2 + bx +c
1 2 1
Output:
Roots are : -1 -1
Time Complexity : O(1)
Space Complexity : O(1)
'''