Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 11-28-2006, 10:07 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Some of you guys sure do like to take advantage of Graal to do your homework
Reply With Quote
  #3  
Old 11-29-2006, 04:28 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Make is complete the squares! ;o

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

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #4  
Old 11-29-2006, 05:01 AM
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
Quote:
Originally Posted by coreys View Post
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.
Reply With Quote
  #5  
Old 11-29-2006, 09:29 AM
contiga contiga is offline
Graal2001 Administration
contiga's Avatar
Join Date: Jul 2004
Location: Netherlands
Posts: 419
contiga is an unknown quantity at this point
Send a message via ICQ to contiga Send a message via AIM to contiga Send a message via MSN to contiga Send a message via Yahoo to contiga
"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
__________________
AIM: Contiga122
MSN: [email protected]
Status:
Quote:
Originally Posted by unixmad View Post
I am also awake 3AM to help correct problems.
Quote:
Originally Posted by Bomy Island RC people
Daniel: HoudiniMan is a bad guy =p
*Bell: rofl. I first read that as houdini is a bad man. like the little kid that wants his mommy to keep her away from that boogie man
Daniel: xD
*Rufus: I wouldn't want my kids around him.
Reply With Quote
  #6  
Old 11-30-2006, 03:37 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Quote:
Originally Posted by Yen View Post
(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
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #7  
Old 11-29-2006, 10:08 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
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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:51 PM.


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