Yen

11-28-2006, 09:57 PM

Call 'onFactorSynDiv' and pass it an array of the coefficients.

i.e. 'x^2 + 7x + 12' would be passed as {1,7,12}

The NPC only uses synthetic division; I had planned to try to make it use other methods (common factors, difference of cubes/squares, sum of cubes, ect.), but I didn't feel like it.

function onFactorSynDiv(equation) {

this.result = "";

this.equation = equation;

this.oequation = equation;

this.factors = "";

temp.checkfactor = this.equation[this.equation.size()-1];

for (a = abs(temp.checkfactor)*-1; a <= abs(temp.checkfactor); a++) {

if ((temp.checkfactor / a) == int(temp.checkfactor / a)) {

if (a == 0) continue;

if (temp.used.index(temp.checkfactor / a) == -1) {

this.factors.add(temp.checkfactor / a);

temp.used.add(temp.checkfactor / a);

}

}

}

onSyntheticDivision(this.factors[0],this.equation);

}

function NextDiv() {

this.factors.delete(0);

if (this.factors.size() > 0) onSyntheticDivision(this.factors[0],this.equation);

else {

if (this.result == "") {

echo(ParseEquation(this.equation) SPC "cannot be factored by this method.");

return;

}

echo(ParseEquation(this.oequation));

echo("Divided down to" SPC this.result);

if (this.equation.size() > 0) {

if (this.equation[0] != 1) {

echo(ParseEquation(this.equation) SPC "is left over.");

}

}

echo("Result:" SPC this.result @ ParseEquation(this.equation));

}

}

function onSyntheticDivision(factor,equation) {

if ((equation[equation.size()-1] / factor) != int(equation[equation.size()-1] / factor)) {

NextDiv();

return;

}

temp.arr1 = new[equation.size()];

temp.arr2 = new[equation.size()];

temp.arr2[0] = equation[0];

for (a = 1; a < equation.size(); a++) {

temp.arr1[a] = temp.arr2[a-1] * factor;

temp.arr2[a] = temp.arr1[a] + equation[a];

}

if (temp.arr2[temp.arr2.size()-1] == 0) {

temp.arr2.delete(temp.arr2.size()-1);

this.equation = temp.arr2;

this.result @= "(x";

if ((factor*-1) < 0) this.result @= " - ";

else this.result @= " + ";

this.result @= abs(factor) @ ")";

}

NextDiv();

}

function ParseVar(factor) {

if (factor > 0) return "+" @ factor;

else return factor;

}

function ParseEquation(equation) {

for (e = 0; e < equation.size(); e++) {

if (equation[e] == 0) continue;

if (e < equation.size() - 1) {

if (abs(equation[e]) == 1) temp.toreturn @= "x";

else temp.toreturn @= abs(equation[e]) @ "x";

}

else temp.toreturn @= abs(equation[e]);

if (e < equation.size() - 2) temp.toreturn @= "^" @ equation.size() - 1 - e;

if (e < equation.size() - 1) {

if (equation[e] < 0) temp.toreturn @= " - ";

else temp.toreturn @= " + ";

}

}

if (temp.toreturn == 1) return "";

return temp.toreturn;

}

i.e. 'x^2 + 7x + 12' would be passed as {1,7,12}

The NPC only uses synthetic division; I had planned to try to make it use other methods (common factors, difference of cubes/squares, sum of cubes, ect.), but I didn't feel like it.

function onFactorSynDiv(equation) {

this.result = "";

this.equation = equation;

this.oequation = equation;

this.factors = "";

temp.checkfactor = this.equation[this.equation.size()-1];

for (a = abs(temp.checkfactor)*-1; a <= abs(temp.checkfactor); a++) {

if ((temp.checkfactor / a) == int(temp.checkfactor / a)) {

if (a == 0) continue;

if (temp.used.index(temp.checkfactor / a) == -1) {

this.factors.add(temp.checkfactor / a);

temp.used.add(temp.checkfactor / a);

}

}

}

onSyntheticDivision(this.factors[0],this.equation);

}

function NextDiv() {

this.factors.delete(0);

if (this.factors.size() > 0) onSyntheticDivision(this.factors[0],this.equation);

else {

if (this.result == "") {

echo(ParseEquation(this.equation) SPC "cannot be factored by this method.");

return;

}

echo(ParseEquation(this.oequation));

echo("Divided down to" SPC this.result);

if (this.equation.size() > 0) {

if (this.equation[0] != 1) {

echo(ParseEquation(this.equation) SPC "is left over.");

}

}

echo("Result:" SPC this.result @ ParseEquation(this.equation));

}

}

function onSyntheticDivision(factor,equation) {

if ((equation[equation.size()-1] / factor) != int(equation[equation.size()-1] / factor)) {

NextDiv();

return;

}

temp.arr1 = new[equation.size()];

temp.arr2 = new[equation.size()];

temp.arr2[0] = equation[0];

for (a = 1; a < equation.size(); a++) {

temp.arr1[a] = temp.arr2[a-1] * factor;

temp.arr2[a] = temp.arr1[a] + equation[a];

}

if (temp.arr2[temp.arr2.size()-1] == 0) {

temp.arr2.delete(temp.arr2.size()-1);

this.equation = temp.arr2;

this.result @= "(x";

if ((factor*-1) < 0) this.result @= " - ";

else this.result @= " + ";

this.result @= abs(factor) @ ")";

}

NextDiv();

}

function ParseVar(factor) {

if (factor > 0) return "+" @ factor;

else return factor;

}

function ParseEquation(equation) {

for (e = 0; e < equation.size(); e++) {

if (equation[e] == 0) continue;

if (e < equation.size() - 1) {

if (abs(equation[e]) == 1) temp.toreturn @= "x";

else temp.toreturn @= abs(equation[e]) @ "x";

}

else temp.toreturn @= abs(equation[e]);

if (e < equation.size() - 2) temp.toreturn @= "^" @ equation.size() - 1 - e;

if (e < equation.size() - 1) {

if (equation[e] < 0) temp.toreturn @= " - ";

else temp.toreturn @= " + ";

}

}

if (temp.toreturn == 1) return "";

return temp.toreturn;

}