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 watermark_image

function watermark_image ($image, $watermark, $position, $margin, $transparency
{
  $w = imagesx$image);
  $h = imagesy$image);
  
  $ww = imagesx$watermark);
  $hw = imagesy$watermark);
  
  $image2 = image_create_alpha$w, $h);
  imagecopy$image2, $image, 0, 0, 0, 0, $w, $h);
  
  imagealphablending$image2, true);
  imagesavealpha$image2, true);
  
  switch ($position) {
    case 'tl'
      $top = $margin
      $left = $margin
      break
    case 'tr'
      $top = $margin
      $left = $w$ww$margin
      break
    case 'br'
      $top = $h$hw$margin
      $left = $w$ww$margin
      break
    case 'bl'
      $top = $h$hw$margin
      $left = $margin
      break
    default
      $top = ($h$hw)/2;
      $left = ($w$ww)/2;
  }
  
  if ($transparency>0)
    imagecopy$image2, add_transparency$watermark, $transparency), $left, $top, 0, 0, $ww, $hw); 
  else
    imagecopy$image2, $watermark, $left, $top, 0, 0, $ww, $hw);
  
  return $image2
} 

eXorithm – Execute Algorithm: View / Run Algorithm unity

function unity ($passkey
{
  $ubn = array();
  
  while ($passkey > 9) {
    if (strlen$passkey) > 1)
      $passkey = array_sumstr_split$passkey));
    else
      $passkey = $passkey
  } 
  
  $ubn[] = $passkey
  
  return end$ubn);
} 

eXorithm – Execute Algorithm: View / Run Algorithm derivative

function derivative ($function, $with_respect_to
{
  // $with_respect_to is a descriptive variable name, but it's too long!
  $x = $with_respect_to
  
  if (is_array$function)) {
      
    // pop the operator off the front of the equation
    $operator = array_shift$function);
    
    switch ($operator) {
        
      case '+'
        // (f+g)' = f' + g'
        return simplify_equationarray'+', derivative$function[0], $x), derivative$function[1], $x)));
  
      case '-'
        // (f-g)' = f' - g'
        return simplify_equationarray'-', derivative$function[0], $x), derivative$function[1], $x)));
  
      case '*'
        // (f*g)' = f'*g + f*g'
        return simplify_equationarray'+'
                     array'*', derivative$function[0], $x), $function[1]),
                     array'*', $function[0], derivative$function[1], $x))
                    ));
  
      case '/'
        // (f/g)' = (f'*g - f*g') / g^2
        return simplify_equationarray'/'
                     array'-'
                           array'*', derivative$function[0], $x), $function[1]),
                           array'*', $function[0], derivative$function[1], $x))
                          ),
                     array'^', $function[1], 2)
                    ));
  
      case '^'
        // (f^g)' = (f^g) * (g'*ln(f) + (g/f)*f')
        return simplify_equationarray'*'
                     array'^', $function[0], $function[1]),
                     array'+'
                           array'*'
                                 derivative$function[1], $x),
                                 array'ln', $function[0])
                                ),
                           array'*'
                                 array'/', $function[1], $function[0]),
                                 derivative$function[0], $x
                                )
                          ),
                    ));
        
      case 'neg'
        // (-f') = -(f')
        return simplify_equationarray'neg', derivative$function[0], $x)));
    
      case 'sqrt'
        // (f^(1/2))' = (f^(1/2)) * ((1/2f)*f')
        return simplify_equationarray'*'
                     array'sqrt', $function[0]),
                     array'*'
                            array'/', 1, array'*', 2, $function[0])),
                            derivative$function[0], $x
                          )
                    ));
  
      case 'log'
        // (log(f,g))' = (1/(f*ln(g))) * f'
        return simplify_equationarray'*'
                     array'/', 1, array'*'
                                         $function[0],
                                         array'ln', $function[1])
                                         )),
                     derivative$function[0], $x
                    ));
  
      case 'ln'
        // (ln(f))' = (1/f) * f'
        return simplify_equationarray'*'
                     array'/', 1, $function[0]),
                     derivative$function[0], $x
                    ));
  
      case 'exp'
        // (exp(f))' = exp(f)
        return simplify_equationarray'exp', $function[0]));
  
      case 'root'
        // (f^(1/g))' = (f^(1/g)) * ((1/g)'*ln(f) + (1/fg)*f')
        return simplify_equationarray'*'
                     array'^', $function[0], array'/', 1, $function[1])),
                     array'+'
                           array'*'
                                 derivativearray'/', 1, $function[1]), $x),
                                 array'ln', $function[0])
                                ),
                           array'*'
                                 array'/', array'/', 1, $function[1]), $function[0]),
                                 derivative$function[0], $x
                                )
                          ),
                    ));
        
        // trig
        // these all use the chain rule (f(g(x)))' = f'(g(x)) * (g(x))'
        
      case 'sin'
        return simplify_equationarray'*'
                     array'cos', $function[0]),
                     derivative$function[0], $x
                    ));
        
      case 'cos'
        return simplify_equationarray'*'
                     array'neg', array'sin', $function[0])),
                     derivative$function[0], $x
                    ));
  
      case 'tan'
        return simplify_equationarray'*'
                     array'/', 1, array'^', array'cos', $function[0]), 2)),
                     derivative$function[0], $x
                    ));
  
      case 'sec'
         return simplify_equationarray'*'
                     array'*'
                           array'sec', $function[0]),
                           array'tan', $function[0])
                          ),
                     derivative$function[0], $x
                    ));
  
      case 'csc'
         return simplify_equationarray'*'
                     array'*'
                           array'neg', array'csc', $function[0])),
                           array'cot', $function[0])
                          ),
                     derivative$function[0], $x
                    ));
  
      case 'cot'
         return simplify_equationarray'*'
                     array'/', -1, array'^', array'sin', $function[0]), 2)),
                     derivative$function[0], $x
                    ));
        
     // below are not done
        
        // hyperbolic trig
      case 'sinh'
  
      case 'cosh'
  
      case 'tanh'
  
      case 'sech'
  
      case 'csch'
  
      case 'coth'
  
        // arc trig
      case 'arcsin'
  
      case 'arccos'
  
      case 'arctan'
  
        // inverse hyperbolic trig
      case 'arsinh'
  
      case 'arcosh'
  
      case 'artanh'
  
      default
        throw new Exception'usupported operator in derivative '$operator);
    }
  } else {
    if (ctype_alpha$function)) {
      if ($function==$x) {
        return 1;
      } else {
        throw new Exception'function contains a variable other than '$x'!');
      }
    } else {
      return 0;
    }
  }
  return $function
} 

eXorithm – Execute Algorithm: View / Run Algorithm part_of_speech

function part_of_speech ($word
{
  $data = file_get_contents"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q="urlencode$word)."&sl=en&tl=en&restrict=pr%2Cde&client=te");
  
  if ( (substr$data, 0, 25)=="dict_api.callbacks.id100(") &&
       (substr$data, -10)==",200,null)") )
  {
    $data = substr$data, 25, -10);
    $data = str_replace'x22', '"', $data);
    $data = decode_phpstring$data);
    
    $parts = array();
    
    $obj = json_decode$data);
    
    if (isset$obj->primaries)) {
      foreach ($obj->primaries as $prim) {
        $headword = ''
        $part = ''
        foreach ($prim->terms as $term) {
          if ($term->type=="text") {
            $headword = str_replace'·', '', $term->text);
            foreach ($term->labels as $label) {
              if ($label->title=="Part-of-speech") {
                
                $st = false;
                $part = strtolower$label->text);
                
                foreach ($prim->entries as $entry) {
                  if ($entry->type=="related") {
                    foreach ($entry->terms as $term2) {
                      if ($term2->text==$word) {
                        $st = true;
                        $i = count$parts);
                        $parts$i]['type'] = $part
                        $parts$i]['base'] = $headword
                        $parts$i]['subtype'] = $term2->labels[0]->text;
                      }
                    }
                  }
                }
                
                if (!$st) {
                  $i = count$parts);
                  $parts$i]['type'] = $part
                  $parts$i]['base'] = $headword
                  $parts$i]['subtype'] = ''
                }
              }
            }
          }
        }
      }
    }
    return $parts
    
  } else {
    throw new Exception"Google dictionary API did not return a result.");
  }
} 

eXorithm – Execute Algorithm: View / Run Algorithm gcd_euclid

function gcd_euclid ($a, $b
{
  $a = abs$a);
  $b = abs$b);
  
  if ($a == 0)
    return $b
  elseif ($b == 0)
    return $a
  elseif ($a > $b
    return gcd_euclid$b, $a % $b);
  else
    return gcd_euclid$a, $b % $a);
} 

eXorithm – Execute Algorithm: View / Run Algorithm coin_change

function coin_change ($amount, $coins
{
  $change = array();
  rsort$coins);
  for$i=0; $icount$coins); $i++) {
    $change$coins$i]] = floor$amount$coins$i]);
    $amount = $amount % $coins$i];
  }
  return $change
} 

eXorithm – Execute Algorithm: View / Run Algorithm plot_3d_function

function plot_3d_function ($function, $variable1, $variable2, $center_x, $center_y, $range, $z_scale, $width, $detail, $line_color, $surface_color
{
  $x1 = $center_x$range/2;
  $y1 = $center_y$range/2;
  
  $xfinal = $center_x$range/2;
  $yfinal = $center_y$range/2;
  
  // step size
  $step = $range$detail
  
  // all the polygons that make up the graph
  $polygons = array();
  
  // generate the polygons for the graph
  for ($ii=0;$ii<=$detail$ii++) {
    $x$x1+($ii$step);
    for ($jj=0;$jj<=$detail$jj++) {
      $y$y1+($jj$step);
      
      $z = evaluate_equation$function, array$variable1=>$x, $variable2=>$y))*$z_scale
      
      if (!is_nan$z) && !is_infinite$z)) {
        
        if ($ii$detail) {
          if ($jj$detail) {
            $poly = $ii$detail$jj+1;
            $polygons$poly][6] = $x
            $polygons$poly][7] = $y
            $polygons$poly][8] = $z
          }
          
          if ($jj>0) {
            $poly = $ii$detail$jj
            $polygons$poly][9] = $x
            $polygons$poly][10] = $y
            $polygons$poly][11] = $z
          }
        }
        
        if ($ii>0) {
          if ($jj$detail) {
            $poly = ($ii-1)*$detail$jj+1;
            $polygons$poly][3] = $x
            $polygons$poly][4] = $y
            $polygons$poly][5] = $z
          }
          
          if ($jj>0) {
            $poly = ($ii-1)*$detail$jj
            $polygons$poly][0] = $x
            $polygons$poly][1] = $y
            $polygons$poly][2] = $z
          }
        }
      }
    }
  }
  
  $polygons = array_values$polygons);
  
  // project each of the polygons into 2-d
  for ($i=0; $icount$polygons); $i++) {
    $points[] = project_polygon$polygons$i], -60, 180, 135, $center_x, $center_y, 0, $range, $range*2, true);
  }
  
  // scale the image somewhat
  $scale = $width*2/$range
  
  return render_polygons$points, $line_color, $surface_color, false, false, $width, $scale);
}Â