1 
  2 /* *********************************************************
  3     factorpolynomial.js
  4     (c) 2008 by Allen W. Murphy
  5     Code for factoring a second degree polynomial.
  6     GPLv2 
  7    ********************************************************* */
  8 
  9 /* ********************************************************* */
 10 function gcf(A, B, C) {
 11 
 12     /* return the GCF of three integers */
 13 
 14     var i = getMinCoefficient(A, B, C);
 15 
 16     while (i > 1) {
 17         if (Math.abs(A) % i == 0) {
 18             if (Math.abs(B) % i == 0) {
 19                 if (Math.abs(C) % i == 0) {
 20                     return i
 21                 }
 22             }
 23         }
 24         i--;
 25     }
 26     return 1;
 27 }
 28 
 29 /* ********************************************************* */
 30 function getMinCoefficient(A, B, C) {
 31 
 32     /* returns the lowest of three integers */
 33     
 34     if (Math.abs(A) <= Math.abs(B) && Math.abs(A) <= Math.abs(C)) {
 35         return A;
 36     }
 37     else if (Math.abs(B) <= Math.abs(A) && Math.abs(B) <= Math.abs(C)) {
 38         return B;
 39     }
 40     else {
 41         return C;
 42     }
 43 }
 44 
 45 /* ********************************************************* */
 46 function getFactorPairs(n) {
 47     
 48     /* returns an array f factor pairs of an integer */
 49     
 50     var i = 0;
 51     var pairs = new Array();
 52     
 53     /* Positive factors */
 54     for (var f = 1; f <= Math.abs(n); f++) {
 55         if (Math.abs(n) % f == 0) {
 56             pairs[i] = {x: f, y: n/f};
 57             //alert(pairs[i].x + ", " + pairs[i].y);
 58             i++;
 59         }
 60     }
 61 
 62     /* Negative factors */
 63     for (var f = -1*Math.abs(n); f <= -1; f++) {
 64         if (Math.abs(n) % f == 0) {
 65             pairs[i] = {x: f, y: n/f};
 66             //alert(pairs[i].x + ", " + pairs[i].y);
 67             i++;
 68         }
 69     }
 70     
 71     return pairs;
 72 }
 73 
 74 /* ********************************************************* */
 75 function factorPolynomial(output, A, B, C) {
 76 
 77     /* factor a second degree polynomial Ax^2 + Bx + C */
 78 
 79 
 80     var firstoperator = "+";
 81     var secondoperator = "+";
 82 
 83     // Factor out constants
 84     var h = gcf(A, B, C);
 85 
 86     //alert(h);
 87     
 88     if (h != 1 && h != 0) {
 89         A = A/h;
 90         B = B/h;
 91         C = C/h;
 92         output += "Factor out " + h + " leaving <b>" + h + "(" + A + "x^2 + " +
 93             B + "x + " + C +") </b><br /><br />";
 94     }
 95 
 96     var aPairs = getFactorPairs(A);
 97     var cPairs = getFactorPairs(C);
 98     var found = 0;
 99 
100     output += "Check factors of (" + A + ")(" + C + ") = " + A*C +
101         " so they sum to " + B + ".<br />"; 
102 
103     for (var i=0; i < aPairs.length; i++) {
104         for (var j=0; j < cPairs.length; j++) {
105 
106             if (aPairs[i].x * cPairs[j].y + aPairs[i].y * cPairs[j].x == B) {
107                 // yes we found the factors! 
108                 // display h(aPairs.x x + cPairs.x)(aPairs.y x + cPairs.y)
109 
110                 output += '<span class="boldblue">' +
111                     aPairs[i].x * cPairs[j].y + " + " + (aPairs[i].y * cPairs[j].x) + 
112                     " = " + (aPairs[i].x * cPairs[j].y + aPairs[i].y * cPairs[j].x) + 
113                     " YES! </span><br />";
114 
115                 output += '<br />So, ' + 'our outer factors multiply to <b>' + aPairs[i].x * cPairs[j].y + 
116                     '</b> and our inner factors multiply to <b>' + aPairs[i].y * cPairs[j].x + 
117                     '</b>.<br />';
118 
119                 output += 'How about <b>' + aPairs[i].x + " * " + cPairs[j].y + "</b> and <b>" +
120                     aPairs[i].y + " * " + cPairs[j].x + "</b>?<br />";
121 
122                 output += 'The coefficients of the first terms need to multiply to ' + 
123                     A + " and the coefficients of the last terms need to " + 
124                     "multiply to " + C + ", so the factored form is: <br /><br />";
125 
126                 /* Coefficients of 1 are not necessary */
127                 if (aPairs[i].x == 1) {
128                     var D = "";
129                 }
130                 else {
131                     var D = aPairs[i].x;
132                 }               
133                 
134                 if (aPairs[i].y == 1) {
135                     var E = "";
136                 }
137                 else {
138                     var E = aPairs[i].y;
139                 }               
140                 
141                 /* Set the operators to + or - */
142                 if (cPairs[j].x < 0) {
143                     firstoperator = "-";
144                 }
145                 else {
146                     firstoperator = "+";
147                 }
148                 
149                 if (cPairs[j].y < 0) {
150                     secondoperator = "-";
151                 }
152                 else {
153                     secondoperator = "+";
154                 }
155                 
156                 /* Add factored form to output. */
157                 output += '<span class="boldred">';
158                 if (h == 1) {
159                     output += "(" + D + "x "+ firstoperator + " " + 
160                         Math.abs(cPairs[j].x) + ")(" + E + "x " +
161                         secondoperator + " " + Math.abs(cPairs[j].y) +")";
162                 }
163                 else {
164                     output += h + "(" + D + "x "+ firstoperator + " " + 
165                         Math.abs(cPairs[j].x) + ")(" + E + "x " + 
166                         secondoperator + " " + Math.abs(cPairs[j].y) +")";
167                 }
168                 output += '</span><br />';
169                 found = 1;
170                 break; 
171             }
172             output += aPairs[i].x * cPairs[j].y + " + " + 
173                 aPairs[i].y * cPairs[j].x + " = " + (aPairs[i].x * cPairs[j].y +
174                 aPairs[i].y * cPairs[j].x) + " NO <br />";
175                 
176         }
177         if (found == 1) {
178             break;
179         }
180     }
181     if (found == 0) {
182         output = "Prime";
183     }
184     return output;
185 }
186 
187 /* ********************************************************* */
188 function validateCoefficients(A, B, C) {
189 
190     if (isNaN(A) || isNaN(B) || isNaN(C)) {
191         alert("One or more of your coefficients is not a number.");
192         return false;
193     }
194     else if ((A % 1 != 0) || (B % 1 != 0) || (C % 1 != 0)) {
195         alert("One or more of your coefficients is not an integer.");
196         return false;
197     }
198     else {
199         return true;
200     }   
201 }
202 
203 /* ********************************************************* */
204 function solveClicked() {
205 
206     /* Called when the user clicks the "Solve" button.
207        This is the main funciton of the script.*/
208     
209     var A = document.polynomialform.A.value;
210     var B = document.polynomialform.B.value;
211     var C = document.polynomialform.C.value;
212     var output = "";
213     
214     if (validateCoefficients(A, B, C)) {    
215         output = factorPolynomial(output, A, B, C);
216         document.getElementById("steps").innerHTML = output;
217     }
218 }
219 
220 /*
221  This script is free software; you can redistribute it and/or modify
222  it under the terms of the GNU General Public License as published by
223  the Free Software Foundation; either version 2 of the License, or
224  (at your option) any later version.
225 
226  This script is distributed in the hope that it will be useful,
227  but WITHOUT ANY WARRANTY; without even the implied warranty of
228  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
229  GNU General Public License for more details.
230 
231  To contact the FSF, write to the Free Software
232  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
233 */


syntax highlighted by Code2HTML, v. 0.9.1