Your mathematics teacher is a big fan of Vieta's formulas, and he believes that you should use them to solve quadratic equations. Given the equation
ax^2 + bx + c = 0
the product of its roots is
c/a, and their sum is
-b/a. When all of
c are nonzero integers, assuming the roots are rational numbers, it's enough to try all possible numbers in the form
r1 = ±s/t
s is a divisor of
t is a divisor of
abs(a). For each such
r1, plug it into
ax^2 + bx + c, and see whether the result is 0. If yes, then
r1 is a root. The second root is
(c/a)/r1 - you can choose whatever formula you like.
Your teacher decided to give you many exercises, and he expects you to describe how you used Vieta's formulas to solve each one. Each exercise looks like this (example):
Write a subroutine or a program that gets an exercise as input, and outputs your alleged "solving process" to appease your teacher.
Since you will feed the exercise to your program manually, format it in any convenient form. For example, use space-separated values on
9 12 4
or call a function with 3 parameters:
SolveExercise(9, 12, 4);
or parse the exercise literally:
Your output should be formatted as described below. Use the standard output device or return it as a string from your subroutine.
x = 1? 9x^2+12x+4 = 25 x = 2? 9x^2+12x+4 = 64 x = 1/3? 9x^2+12x+4 = 9 x = 2/3? 9x^2+12x+4 = 16 ... (as many or as few failed attempts as you like) x = -2/3? 9x^2+12x+4 = 0 r1 = -2/3 r2 = -12/9-(-2/3) = -2/3
Alternatively, the last line can be:
r2 = 4/9/(-2/3) = -2/3
Some additional notes:
- The minimum number of line-breaks in the output is as described in the example (trailing line-break is not required). Additional line-breaks are permitted.
- All coefficients in input are integers in the range [-9999...9999], none can be equal to 0
- All roots are rational numbers, and should be output as such - e.g.
0.66666667is not equal to
2/3and so is incorrect
- In the final expressions for
r2, integers should be output as such, e.g.
-99/1is unacceptable, and should be output as
-99; in other places in the output, denominator equal to ±1 is acceptable
- Reduced form for rational numbers is not required - e.g.
2/4is a good substitute for
1/2, even though it's ugly, even for roots
- Parentheses in the output are sometimes required by rules of mathematics, e.g. in the expression
12/9/(2/3). When precedence rules of mathematics permit omission of parentheses, they are not required, e.g.
-12/9--2/3. Superfluous parentheses are permitted:
4-(2)is OK, even though it's ugly
- There should be at least one case (input) for which your program tries 3 or more non-integer values for
r1; however, it's allowed to "guess the right answer" almost always on the first try
- All trial values for
r1must be rational numbers
tare constrained as described above
1 -1 -2
x=1? x^2-x-2=-2 x=-1? x^2-x-2=0 r1=-1 r2=-2/1/-1=2
-1, 2x, -1
x=1? -x^2+2x-1=0 r1=1 r2=-2/-1-1=1
X(7, -316, 924);
Possible output (a divisor of 924 is 42, which solves the equation by "luck")
x=42? 7x^2-316x+924=0 r1=42 r2=316/7-42=22/7
[6 -35 -6]
Possible output (even though your program may "know" that 6 is a root, it decides to show some failed trials)
x=1/2? 6x^2-35x-6=-88/4 x=1/3? 6x^2-35x-6=-153/9 x=3/2? 6x^2-35x-6=-180/4 x=6/1? 6x^2-35x-6=0 r1=6 r2=35/6-6=-1/6
Alternative versions for the last line:
Impossible input (no rational solutions)
Impossible input (zero coefficient)