Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Factoring NPC (https://forums.graalonline.com/forums/showthread.php?t=70455)

Yen 11-28-2006 09:57 PM

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;



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

Quote:

Originally Posted by coreys (Post 1248364)
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

Quote:

Originally Posted by Yen (Post 1248377)
(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


All times are GMT +2. The time now is 03:42 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.