function reverse_factorial ($factorial
{
$current = 1;
while ($factorial > $current) {
if ($factorial % $current) return -1;
$factorial = $factorial$current
$current++;
}
if ($factorial == $current) return $current
else return -1;
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm simplify_equation
function simplify_equation ($equation
{
if (is_array$equation)) {
// first simply the parameters of the equation
$equation[1] = simplify_equation$equation[1]);
if (isset$equation[2])) {
$equation[2] = simplify_equation$equation[2]);
}
// simplify based on the operator
switch ($equation[0]) {
case '+'
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
return $equation[1]+$equation[2];
}
if ($equation[1]===0) {
return $equation[2];
}
if ($equation[2]===0) {
return $equation[1];
}
return $equation
case '-'
if ($equation[1]===$equation[2]) {
return 0;
}
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
return $equation[1]-$equation[2];
}
if ($equation[1]===0) {
return array'neg', $equation[2]);
}
if ($equation[2]===0) {
return $equation[1];
}
return $equation
case '*'
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
return $equation[1]*$equation[2];
}
if ($equation[1]===0) {
return 0;
}
if ($equation[2]===0) {
return 0;
}
if ($equation[1]===1) {
return $equation[2];
}
if ($equation[2]===1) {
return $equation[1];
}
return $equation
case '/'
if ($equation[2]===0) {
throw new Exception'division by zero');
}
if ($equation[1]===$equation[2]) {
return 1;
}
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
if (($equation[1] % $equation[2])==0) {
return $equation[1]/$equation[2];
} else {
// get the greastest common divisor
$gcd = gcd_euclid$equation[1], $equation[2]);
$equation[1] = $equation[1]/$gcd
$equation[2] = $equation[2]/$gcd
}
}
if ($equation[1]===0) {
return 0;
}
if ($equation[2]===1) {
return $equation[1];
}
return $equation
case '^'
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
return pow$equation[1], $equation[2]);
}
if ($equation[1]===0) {
return 0;
}
if ($equation[1]===1) {
return 1;
}
if ($equation[2]===0) {
return 1;
}
if ($equation[2]===1) {
return $equation[1];
}
return $equation
case 'neg'
if (is_array$equation[1])) {
if ($equation[1][0]=='neg') {
return $equation[1][1];
}
}
if (is_numeric$equation[1])) {
return $equation[1]*-1;
}
return $equation
case 'sqrt'
if (is_numeric$equation[1])) {
return sqrt$equation[1]);
}
return $equation
case 'log'
if ($equation[1]===1) {
return 0;
}
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
return log$equation[1], $equation[2]);
}
return $equation
case 'ln'
if (is_numeric$equation[1])) {
return log$equation[1]);
}
return $equation
case 'exp'
if (is_numeric$equation[1])) {
return exp$equation[1]);
}
return $equation
case 'root'
if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
return pow$equation[1], 1/$equation[2]);
}
if ($equation[1]===0) {
return 0;
}
if ($equation[1]===1) {
return 1;
}
if ($equation[2]===0) {
throw new Exception'division by zero');
}
if ($equation[2]===1) {
return $equation[1];
}
return $equation
// trig
case 'sin'
if (is_numeric$equation[1])) {
return sin$equation[1]);
}
return $equation
case 'cos'
if (is_numeric$equation[1])) {
return cos$equation[1]);
}
return $equation
case 'tan'
if (is_numeric$equation[1])) {
return tan$equation[1]);
}
return $equation
case 'sec'
if (is_numeric$equation[1])) {
return 1/cos$equation[1]);
}
return $equation
case 'csc'
if (is_numeric$equation[1])) {
return 1/sin$equation[1]);
}
return $equation
case 'cot'
if (is_numeric$equation[1])) {
return 1/tan$equation[1]);
}
return $equation
// hyperbolic trig
case 'sinh'
if (is_numeric$equation[1])) {
return sinh$equation[1]);
}
return $equation
case 'cosh'
if (is_numeric$equation[1])) {
return cosh$equation[1]);
}
return $equation
case 'tanh'
if (is_numeric$equation[1])) {
return tanh$equation[1]);
}
return $equation
case 'sech'
if (is_numeric$equation[1])) {
return 1/cosh$equation[1]);
}
return $equation
case 'csch'
if (is_numeric$equation[1])) {
return 1/sinh$equation[1]);
}
return $equation
case 'coth'
if (is_numeric$equation[1])) {
return 1/tanh$equation[1]);
}
return $equation
// arc trig
case 'arcsin'
if (is_numeric$equation[1])) {
return asin$equation[1]);
}
return $equation
case 'arccos'
if (is_numeric$equation[1])) {
return acos$equation[1]);
}
return $equation
case 'arctan'
if (is_numeric$equation[1])) {
return atan$equation[1]);
}
return $equation
// inverse hyperbolic trig
case 'arsinh'
if (is_numeric$equation[1])) {
return asinh$equation[1]);
}
return $equation
case 'arcosh'
if (is_numeric$equation[1])) {
return acosh$equation[1]);
}
return $equation
case 'artanh'
if (is_numeric$equation[1])) {
return atanh$equation[1]);
}
return $equation
default
throw new Exception'usupported operator '$operator);
}
}
return $equation
}
eXorithm – Execute Algorithm: View / Run Algorithm peasant_multiply
function peasant_multiply ($a, $b
{
$c = 0;
while$a>0)
{
if(($a%10)%2 != 0)
$c += $b
$a = $a >> 1;
$b = $b << 1;
}
return $c
}
eXorithm – Execute Algorithm: View / Run Algorithm fibonacci_binet
function fibonacci_binet ($n
{
$phi = (1 + sqrt(5)) / 2;
$u = (pow$phi, $n) - pow(1 - $phi, $n)) / sqrt(5);
return $u
}
eXorithm – Execute Algorithm: View / Run Algorithm random_list
function random_list ($start, $end
{
  $array = range$start, $end);
 Â
  usort$array, 'random_usort');
 Â
  return $array
}Â
eXorithm – Execute Algorithm: View / Run Algorithm distance_on_earth
function distance_on_earth ($point1, $point2
{
$radius = 6371; // average radius of the earth in km
$lat1 = deg2rad$point1'latitude']);
$lat2 = deg2rad ($point2'latitude']);
$long1 = deg2rad ($point1'longitude']);
$long2 = deg2rad ($point2'longitude']);
return $radius * acossin$lat1)*sin$lat2) + cos$lat1)*cos$lat2) * cos$long2$long1));
}
eXorithm – Execute Algorithm: View / Run Algorithm fibonacci_recurse
function fibonacci_recurse ($n
{
  if ($n == 0) {
    return 0;
  }
  elseif ($n == 1 || $n == -1) {
    return 1;
  }
 Â
  if$n > 0) {
    $tot = fibonacci_recurse$n-1) + fibonacci_recurse$n-2);
  }
  else {
    $tot = fibonacci_recurse$n+2) - fibonacci_recurse$n+1);
  }
 Â
  return $tot
}Â
eXorithm – Execute Algorithm: View / Run Algorithm round_corners
function round_corners ($image, $radius, $color, $transparency
{
$width = imagesx$image);
$height = imagesy$image);
$image2 = imagecreatetruecolor$width, $height);
imagesavealpha$image2, true);
imagealphablending$image2, false);
imagecopy$image2, $image, 0, 0, 0, 0, $width, $height);
$full_color = allocate_color$image2, $color, $transparency);
// loop 4 times, for each corner...
for ($left=0;$left<=1;$left++) {
for ($top=0;$top<=1;$top++) {
$start_x = $left * ($width$radius);
$start_y = $top * ($height$radius);
$end_x = $start_x$radius
$end_y = $start_y$radius
$radius_origin_x = $left * ($start_x-1) + (!$left) * $end_x
$radius_origin_y = $top * ($start_y-1) + (!$top) * $end_y
for ($x$start_x$x$end_x$x++) {
for ($y$start_y$y$end_y$y++) {
$dist = sqrtpow$x$radius_origin_x,2)+pow$y$radius_origin_y,2));
if ($dist>($radius+1)) {
imagesetpixel$image2, $x, $y, $full_color);
} else {
if ($dist$radius) {
$pct = 1-($dist$radius);
$color2 = antialias_pixel$image2, $x, $y, $full_color, $pct);
imagesetpixel$image2, $x, $y, $color2);
}
}
}
}
}
}
return $image2
}
eXorithm – Execute Algorithm: View / Run Algorithm date_difference
function date_difference ($date1, $date2
{
  $seconds = abs$date2$date1);
  $text = ""
 Â
  $weeks = floor$seconds/(7*24*60*60));
  if ($weeks>0) $text .= "$weeks weeks, "
  $seconds = $seconds - ($weeks * 7*24*60*60);
 Â
  $days = floor$seconds/(24*60*60));
  if ($days>0) $text .= "$days days, "
  $seconds = $seconds - ($days * 24*60*60);
 Â
  $hours = floor$seconds/(60*60));
  if ($hours>0) $text .= "$hours hours, "
  $seconds = $seconds - ($hours * 60*60);
 Â
  $minutes = floor$seconds/(60));
  if ($minutes>0) $text .= "$minutes minutes, "
  $seconds = $seconds - ($minutes *60);
 Â
  $text .= "$seconds seconds"
   Â
  return $text
}Â
eXorithm – Execute Algorithm: View / Run Algorithm validate_phone
function validate_phone ($number
{
$number = preg_replace"/[().- ]/", '', $number);
$valid = "/^1?[2-9][0-9]{2}[2-9][0-9]{2}[0-9]{4}$/"
if ( preg_match( $valid, $number ) == 1 ) {
return true;
} else {
return false;
}
}