Thread: Factoring NPC
View Single Post
  #1  
Old 11-28-2006, 09:57 PM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
Factoring NPC

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.

PHP Code:
function onFactorSynDiv(equation) {
  
this.result "";
  
this.equation equation;
  
this.oequation equation;
  
this.factors "";
  
temp.checkfactor this.equation[this.equation.size()-1];
  for (
abs(temp.checkfactor)*-1<= abs(temp.checkfactor); a++) {
    if ((
temp.checkfactor a) == int(temp.checkfactor a)) {
      if (
== 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() > 0onSyntheticDivision(this.factors[0],this.equation);
  else {
    if (
this.result == "") {
      echo(
ParseEquation(this.equationSPC "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.equationSPC "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 (
1equation.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) < 0this.result @= " - ";
    else 
this.result @= " + ";
    
this.result @= abs(factor) @ ")";
  }
  
NextDiv();
}

function 
ParseVar(factor) {
  if (
factor 0) return "+" factor;
  else return 
factor;
}

function 
ParseEquation(equation) {
  for (
0equation.size(); e++) {
    if (
equation[e] == 0) continue;
    if (
equation.size() - 1) {
      if (
abs(equation[e]) == 1temp.toreturn @= "x";
      else 
temp.toreturn @= abs(equation[e]) @ "x";
    }
    else 
temp.toreturn @= abs(equation[e]);
    if (
equation.size() - 2temp.toreturn @= "^" equation.size() - e;
    if (
equation.size() - 1) {
      if (
equation[e] < 0temp.toreturn @= " - ";
      else 
temp.toreturn @= " + ";
    }
  }
  if (
temp.toreturn == 1) return "";
  return 
temp.toreturn;

Reply With Quote