Roots of a quadratic function: Difference between revisions
(Quadratic Equation) |
(Python) |
||
Line 51: | Line 51: | ||
The roots are complex: |
The roots are complex: |
||
Root1= (-0.33333,0.47140) Root2= (-0.33333,-0.47140) |
Root1= (-0.33333,0.47140) Root2= (-0.33333,-0.47140) |
||
=={{header|Python}}== |
|||
No attempt is made to categorize the type of output as this is not mentioned as being part of the task. |
|||
<python>>>> def quad_roots(a,b,c): |
|||
a,b,c =complex(a), complex(b), complex(c) |
|||
discriminant = b*b - 4*a*c |
|||
root1 = (-b + discriminant**0.5)/2./a |
|||
return root1, root1.conjugate() |
|||
>>> quad_roots(3, 4, 4/3.) |
|||
((-0.66666666666666663+0j), (-0.66666666666666663-0j)) |
|||
>>> quad_roots(3, 2, -1) |
|||
((0.33333333333333331+0j), (0.33333333333333331-0j)) |
|||
>>> quad_roots(3, 2, 1) |
|||
((-0.33333333333333331+0.47140452079103173j), (-0.33333333333333331-0.47140452079103173j)) |
|||
>>> </python> |
Revision as of 04:44, 28 November 2008
You are encouraged to solve this task according to the task description, using any language you may know.
Write a program to find the roots of a quadratic equation. i.e solve the equation ax2 + bx + c = 0.
The program must correctly handle complex roots. Error checking on the input (for a = 0) need not be shown.
Fortran
PROGRAM QUADRATIC IMPLICIT NONE REAL :: a, b, c, e, discriminant, rroot1, rroot2 COMPLEX :: croot1, croot2 WRITE(*,*) "Enter the coefficients of the equation ax^2 + bx + c" WRITE(*, "(A)", ADVANCE="NO") "a = " READ *, a WRITE(*,"(A)", ADVANCE="NO") "b = " READ *, b WRITE(*,"(A)", ADVANCE="NO") "c = " READ *, c WRITE(*,"(3(A,F10.6))") "Coefficients are: a=", a, " b=", b, " c=", c e = 1e-5 discriminant = b*b - 4*a*c IF (ABS(discriminant) < e) THEN rroot1 = -b / (2*a) WRITE(*,*) "The roots are real and equal:" WRITE(*,*) "Root=", rroot1 ELSE IF (discriminant > 0) THEN rroot1 = (-b + SQRT(discriminant)) / (2*a) rroot2 = (-b - SQRT(discriminant)) / (2*a) WRITE(*,*) "The roots are real:" WRITE(*,*) "Root1=", rroot1, " Root2=", rroot2 ELSE croot1 = (-b + SQRT(CMPLX(discriminant))) / (2*a) croot2 = CONJG(croot1) WRITE(*,*) "The roots are complex:" WRITE(*,*) "Root1=", croot1, " Root2=", croot2 END IF END PROGRAM QUADRATIC
Sample output
Coefficients are: a= 3.000000 b= 4.000000 c= 1.333333 The roots are real and equal: Root= -0.666667 Coefficients are: a= 3.000000 b= 2.000000 c= -1.000000 The roots are real: Root1= 0.333333 Root2= -1.00000 Coefficients are: a= 3.000000 b= 2.000000 c= 1.000000 The roots are complex: Root1= (-0.33333,0.47140) Root2= (-0.33333,-0.47140)
Python
No attempt is made to categorize the type of output as this is not mentioned as being part of the task. <python>>>> def quad_roots(a,b,c): a,b,c =complex(a), complex(b), complex(c) discriminant = b*b - 4*a*c root1 = (-b + discriminant**0.5)/2./a return root1, root1.conjugate()
>>> quad_roots(3, 4, 4/3.) ((-0.66666666666666663+0j), (-0.66666666666666663-0j)) >>> quad_roots(3, 2, -1) ((0.33333333333333331+0j), (0.33333333333333331-0j)) >>> quad_roots(3, 2, 1) ((-0.33333333333333331+0.47140452079103173j), (-0.33333333333333331-0.47140452079103173j)) >>> </python>