function random_list ($start, $end
{
  $array = range$start, $end);
 Â
  usort$array, 'random_usort');
 Â
  return $array
}Â
Tag: Game
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 radtodeg
function radtodeg ($radians
{
  return ($radians * (180.0/pi()));
}Â
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);
}Â