HOW TO SOLVE THE CUBIC
======================


Problem Statement: Write a FORTRAN program to read the coefficient of a cubic equation, find the roots of the polynomial based on the cubic formula, and output the answers. You may have to review your algebra to locate the cubic formula. Algebraic theory on polynomials says that there should be three roots for a cubic equation; complex roots, when exist, will appear in complex conjugate pairs. 

Solution: To find roots to the following cubic equation where a, b, c, and d are real. 

   a*x3 + b*x2 + c*x + d = 0

Formula: 
  Step 1: Calculate p and q
          p = ( 3*c/a - (b/a)2 ) / 3
          q = ( 2*(b/a)3 - 9*b*c/a/a + 27*d/a ) / 27
  Step 2: Calculate discriminant D
          D = (p/3)3 + (q/2)2
  Step 3: Depending on the sign of D, you follow different strategy.
          If D<0, three distinct real roots.
          If D=0, three real roots of which at least two are equal.
          If D>0, one real and two complex roots.
  Step 3a: For D>0 and D=0
          Calculate u and v
          u = cubic_root(-q/2 + sqrt(D))
          v = cubic_root(-q/2 - sqrt(D))
          Find the three transformed roots
          y1 = u + v
          y2 = -(u+v)/2 + i (u-v)*sqrt(3)/2
          y3 = -(u+v)/2 - i (u-v)*sqrt(3)/2
  Step 3b: Alternately, for D<0, a trigonometric formulation is more convenient
          y1 =  2 * sqrt(|p|/3) * cos(phi/3)
          y2 = -2 * sqrt(|p|/3) * cos((phi+pi)/3)
          y3 = -2 * sqrt(|p|/3) * cos((phi-pi)/3)
          where phi = acos(-q/2/sqrt(|p|3/27))
                pi  = 3.141592654...
  Step 4  Finally, find the three roots
          x = y - b/a/3

  Things to watch out for:
    1. Make sure you know what is integer, real, and complex.
    2. FORTRAN's SQRT (square root) function takes only non-negative arguments.
    3. FORTRAN's exponentiation is "**", not "^".
    4. There is no "cubic_root" function in FORTRAN; you do it by raising a
       number to a factional power, e.g., 0.333... for cubic root.
    5. Your can only raise a non-negative number to a fractional power.
       e.g., (-2.)**(1./3.) will not work; you need to issue -(2.)**(1./3.)
       And do not forget the decimal point.  For example, in computing 2.**(1/3),
       FORTRAN evaluates 1/3 first, which is 0, not 0.33... as you might expect.
    6  There is no "|" in FORTRAN.  The absolute value function is "ABS".
    7. FORTRAN is case-insensitive; do not simply use "d" (for coefficient)
       and "D" (for discriminant), as FORTRAN thinks both variables are equivalent.

