![]() |
VOOZH | about |
Given a quadratic equation in the form ax2 + bx + c, (Only the values of a, b and c are provided). We have to find its roots. If the equation has real roots, then print floor value of each root in decreasing order, If the roots are imaginary print Imaginary.
👁 Quadratic EquationsExamples:
Input: a = 1, b = -2, c = 1
Output: [ 1, 1]
Explanation: For a = 1, b = -2, c = 1, the discriminant is 0, so the quadratic equation has one repeated real root: x =−b2 a=1 x = = 1.Input : a = 1, b = 7, c = 12
Output: [ -3, -4]
Explanation: For a = 1, b = 7, c = 12, the discriminant is 49 - 48 = 1, so the roots are real and distinct: so the roots are -3 and -4.Input : a = 1, b = 1, c = 1
Output : Roots are Imaginary
Explanation: For input a = 1, b = 1, c = 1, the discriminant D=b2−4ac=1−4(1)(1)=−3D = b2 - 4ac = 1 - 4(1)(1) = -3, which is negative, so the roots are imaginary (no real roots).
The roots could be found using the below formula (It is known as the formula of Sridharacharya)
The values of the roots depends on the term (b2 - 4ac) which is known as the discriminant (D).
If D > 0:
=> This occurs when b2 > 4ac.
=> The roots are real and unequal.
=> The roots are {-b + (b2 - 4ac)}/2a and {-b - (b2 - 4ac)}/2a.If D = 0:
=> This occurs when b2 = 4ac.
=> The roots are real and equal.
=> The roots are (-b/2a).If D < 0:
=> This occurs when b2 < 4ac.
=> The roots are imaginary and unequal.
=> The discriminant can be written as (-1 * -D).
=> As D is negative, -D will be positive.
=> The roots are {-b ± ?(-1*-D)} / 2a = {-b ± i?(-D)} / 2a = {-b ± i?-(b2 - 4ac)}/2a where i = ?-1.
Use the following pseudo algorithm to find the roots of the
Pseudo algorithm:
Start
Read the values of a, b, c
If a == 0
Print "imaginary" // Not a quadratic equation
Exit'
Compute d = b2 - 4ac // Discriminant
If d > 0
root1 = floor((-b + √d) / (2a))
root2 = floor((-b - √d) / (2a))
Print root1 and root2 in decreasing order
Else If d == 0
root1 = root2 = floor(-b / (2a))
Print root1 and root2
Else
Print "imaginary" // Complex roots
End
Below is the implementation of the above formula.
4 3
Time Complexity: O(1)
Auxiliary Space: O(1)