The following class can be used to find the greatest common factor for two or more numbers by using Euclid’s algorithm. It was created from original code at http://www.calculatorsoup.com/calculators/math/gcf.php.
Quadratic Solver Function for PHP
2010-02-24 by 2 Comments
A PHP function to solve quadratic equations.
/**
* Quadratic Solver Function for PHP
*
* Useage: for 3x^2 + 4x + 5 = 0, use quadratic(3, 4, 5, 'root1') and quadratic(3, 4, 5, 'root2'),
* or simply just quadratic(3, 4, 5, 'both').
* The $root argument doesn't define which value it will be in relation to the other - I could
* easily have called them John and Jane, instead of 1st and 2nd.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/quadratic-solver-function-php/
* @since 2004-03-21
* @version 2011-01-01
*
* @param integer $a
* @param integer $b
* @param integer $c
* @param string $root 'root1', 'root2' or 'both. Default is 'both'
* @param integer $precision Number of decimal place rounding. Default is 3
* @return mixed The root value
*/
function quadratic( $a, $b, $c, $root = 'both', $precision = 3 ) {
$bsmfac = $b * $b - 4 * $a * $c;
if ( $bsmfac < 0 ) { // Accounts for complex roots.
$plusminusone = ' + ';
$plusminustwo = ' - ';
$bsmfac *= - 1;
$complex = (sqrt( $bsmfac ) / (2 * $a));
if ( $a < 0 ) { //if negative imaginary term, tidies appearance.
$plusminustwo = ' + ';
$plusminusone = ' - ';
$complex *= - 1;
} // End if ($a < 0)
$lambdaone = round( -$b / (2 * $a), $precision ) . $plusminusone . round( $complex, $precision ) . 'i';
$lambdatwo = round( -$b / (2 * $a), $precision ) . $plusminustwo . round( $complex, $precision ) . 'i';
} // End if ($bsmfac < 0)
else if ( $bsmfac == 0 ) { // Simplifies if b^2 = 4ac (real roots).
$lambdaone = round( -$b / (2 * $a), $precision );
$lambdatwo = round( -$b / (2 * $a), $precision );
} // End else if (bsmfac == 0)
else { // Finds real roots when b^2 != 4ac.
$lambdaone = (-$b + sqrt( $bsmfac )) / (2 * $a);
$lambdaone = round( $lambdaone, $precision );
$lambdatwo = (-$b - sqrt( $bsmfac )) / (2 * $a);
$lambdatwo = round( $lambdatwo, $precision );
} // End else
// Return what is asked for.
if ( 'root1' == $root ) {
return $lambdaone;
}
if ( 'root2' == $root ) {
return $lambdatwo;
}
if ( 'both' == $root ) {
return $lambdaone . ' and ' . $lambdatwo;
}
}
Quadratic Solver in JavaScript
2010-02-24 by Leave a Comment
One of my earlier scripts. Could be tidied a fair bit now!
/**
* Rounds number to "dp" decimal places.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/category/javascript/
*/
function chop(number, decimal_places)
{
var multiplier = Math.pow(10, decimal_places) // makes multiplier = 10^decimal_places.
number = ( Math.round( number * multiplier ) ) / multiplier;
if ( document.layers ){ // Tidies Netscape 4.x appearance.
if ( number < 1 && number >= 0 ) number = "0" + number; // makes .752 to 0.752
if ( number < 0 && number >-1 ) number = "-0" + number * -1 // makes -.367 to -0.367
}
return number;
}
/**
* Finds two roots.
*
* For ax^2 + bx + c = 0, use quadratic("a", "b", "c", "+") and quadratic("a", "b", "c", "-").
*/
function quadratic(aq, bq, cq, root)
{
var complex,
lambda,
lambdaone,
lambdatwo,
plusminusone,
plusminustwo,
bsmfac = bq * bq - 4 * aq * cq,
precision = 3;
if ( bsmfac < 0 ) { // Accounts for complex roots.
plusminusone = " + ";
plusminustwo = " - ";
bsmfac *= -1;
complex = Math.sqrt( bsmfac ) / ( 2 * aq );
if ( aq < 0 ){ // if negative imaginary term, tidies appearance.
plusminusone = " - ";
plusminustwo = " + ";
complex *= -1;
}
lambdaone = chop( -bq / ( 2 * aq ), precision ) + plusminusone + chop( complex, precision ) + 'i';
lambdatwo = chop( -bq / ( 2 * aq ), precision ) + plusminustwo + chop( complex, precision ) + 'i';
} else if ( 0 == bsmfac ){ // Simplifies if b^2 = 4ac (real roots).
lambdaone = chop( -bq / ( 2 * aq ), precision );
lambdatwo = chop( -bq / ( 2 * aq ), precision );
} else { // Finds real roots when b^2 != 4ac.
lambdaone = (-bq + (Math.sqrt( bsmfac ))) / ( 2 * aq );
lambdaone = chop( lambdaone, precision );
lambdatwo = (-bq - (Math.sqrt( bsmfac ))) / ( 2 * aq );
lambdatwo = chop( lambdatwo, precision );
}
( '+' == root ) ? lambda = lambdaone : lambda = lambdatwo;
return lambda; // Returns either root based on parameter "root" passed to function.
}