PDA

View Full Version : Factoring NPC


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;
}

Gambet
11-28-2006, 10:07 PM
Some of you guys sure do like to take advantage of Graal to do your homework >_<

coreys
11-29-2006, 04:28 AM
Make is complete the squares! ;o

ie: change x^2+2x+4 to (x+2)^2

Yen
11-29-2006, 05:01 AM
Make is complete the squares! ;o

ie: change x^2+2x+4 to (x+2)^2

(x+2)^2 = x^2 + 4x + 4
O_o
Someone fails.

contiga
11-29-2006, 09:29 AM
"33.5x^2 - 1.5x + 4 cannot be factored by this method."
I inserted -33.5, so I also don't see why you abs it?
Whilst my easy ABC theory can (Result: 0.368659933 or -0.323883813)

Edit: Oh wait, that's not the point of it.. well anyway, it should also calculate what 'x' is :P

Yen
11-29-2006, 10:08 PM
It seems that it's putting negatives in the next highest degree if you call it with a '/npc' command, for some reason.. It wasn't doing this when I first made it, weird.

You take the negative and positive of each factor of the lowest degree (reason abs is used in the script) and divide them by the negative and positive of each factor of the highest degree, then do synthetic division of the top numbers by each of the bottom numbers. If the end result is 0, you have a factor of the equation.

coreys
11-30-2006, 03:37 AM
(x+2)^2 = x^2 + 4x + 4
O_o
Someone fails.

Only when I don't bother to work it out, like you obviously did. XD