function pi_digits ($digits
{
$n = floor$digits * 14/4);
$scale = 10000;
$init = 2000;
$carry = 0;
$result = ''
for$i=0;$i<=$n$i++) {
$arr$i] = $init
}
for$i$n$i>0;$i$i-14) {
$sum = 0;
for$j$i$j>0;$j--) {
$sum = ($sum * $j) + ($scale * $arr$j]);
$arr$j] = $sum % (($j*2)-1);
$sum = floor$sum / (($j*2)-1));
}
$result .= sprintf"%04d", ($carry + ($sum / $scale)));
$carry = $sum % $scale;
}
if ($digits>1) {
return $result[0].'.'substr$result,1,$digits-1);
} else {
return substr$result,0,$digits);
}
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm collection_from_freebase
function collection_from_freebase ($topic, $collection
{
// construct the query
$simplequery = array'id'=>$topic, $collection=>array());
// issue the query
$results = freebase_query$simplequery);
// get the result
if (isset$results[0][$collection])) {
$array = $results[0][$collection];
} else {
$array = array(); // not there? return empty array
}
// sort and return
sort$array);
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm even_number
function even_number ($number
{
return (($number%2)==0);
}
eXorithm – Execute Algorithm: View / Run Algorithm weather_forecast
function weather_forecast ($location, $units, $header_color, $day_header_color, $day_color
{
$weather = file_get_contents'http://www.google.com/ig/api?weather='urlencode$location));
$xml = simplexml_load_string$weather);
if (!isset$xml->weather->forecast_conditions)) {
throw new Exception'Data could not be retreived for location '$location);
}
$location = $xml->weather->forecast_information->city['data'];
// four day outlook
for ($i = 0; $i < 4; $i++){
if ($xml->weather->forecast_conditions->$i) {
$forecast_day[] = $xml->weather->forecast_conditions->$i->day_of_week['data'];
$forecast_condition[] = $xml->weather->forecast_conditions->$i->condition['data'];
$low = $xml->weather->forecast_conditions->$i->low['data'];
if ($units=='k'
$low = round((($low - 32)*5/9) + 273, 1);
else if ($units=='c'
$low = round(($low - 32)*5/9, 1);
$forecast_low[] = $low
$high = $xml->weather->forecast_conditions->$i->high['data'];
if ($units=='k'
$high = round((($high - 32)*5/9) + 273, 1);
else if ($units=='c'
$high = round(($high - 32)*5/9, 1);
$forecast_high[] = $high
$forecast_icon[] = $xml->weather->forecast_conditions->$i->icon['data'];
}
}
// current
$condition = $xml->weather->current_conditions->condition['data'];
$temp = $xml->weather->current_conditions->temp_f['data'];
if ($units=='k'
$temp = round((($temp - 32)*5/9) + 273, 1);
else if ($units=='c'
$temp = round(($temp - 32)*5/9, 1);
$icon = $xml->weather->current_conditions->icon['data'];
// build the HTML
$header = "<tr><td colspan=""count$forecast_day)."" bgcolor="#$header_color">"
if ($icon!=''
$header .= "<img align="left" src="http://www.google.com$icon">"
$header .= "<b>$location</b><br>Currently <i>$condition</i> <b>$temp$units</b>"
$header .= "</td></tr>n<tr>"
$data = "<tr>n"
for ($i = 0; $i < count$forecast_day); $i++){
$header .= "<td width="130" bgcolor="#$day_header_color"><b>$forecast_day[$i]</b></td>"
$data .= "<td bgcolor="#$day_color">"
$data .= "<img src="http://www.google.com$forecast_icon[$i]">"
$data .= "<br><i>$forecast_condition[$i]</i>"
$data .= "<br>high <b>$forecast_high[$i]$units</b>"
$data .= "<br>low <b>$forecast_low[$i]$units</b>"
$data .= "</td>n"
}
$header .= "</tr>"
$data .= "</tr>"
return "<table cellpadding="5" cellspacing="3">n$headern$datan</table>"
}
eXorithm – Execute Algorithm: View / Run Algorithm geonames_populations
function geonames_populations ($countries
{
$data = file_get_contents"http://www.geonames.org/countryInfo?");
if ($data==false) {
throw new Exception"Data was not returned by geonames.org");
} else {
// parse the XML
$obj = simplexml_load_string$data);
// put countries in lower case
for ($ii=0;$iicount$countries); $ii++) {
$countries$ii]=strtolower$countries$ii]);
}
// get the population data for the matching countries
$all_countries = $obj->country;
$valuesarray();
foreach ($all_countries as $country) {
if (array_searchstrtolower$country->countryName), $countries)!==false) {
$country_name = $country->countryName.'';
$country_pop = $country->population+0;
$values$country_name] = $country_pop
}
}
return $values
}
}
eXorithm – Execute Algorithm: View / Run Algorithm validate_url
function validate_url ($url
{
$regex = '/^(https?|ftp)://'; //protocol
$regex .= '(([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+'; //username
$regex .= '(:([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+)?'; //password
$regex .= '@)?'; //auth requires @
$regex .= '((([a-z0-9][a-z0-9-]*[a-z0-9].)*'; //domain segments AND
$regex .= '[a-z][a-z0-9-]*[a-z0-9]'; //top level domain OR
$regex .= '|((d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5]).){3}'
$regex .= '(d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5])'; //IP address
$regex .= ')(:d+)?'; //port
$regex .= ')(((/+([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)*'; //path
$regex .= '(?([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)'; //query string
$regex .= '?)?)?'; //path and query string optional
$regex .= '(#([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)?'; //fragment
$regex .= '$/i'
return (preg_match$regex, $url) ? true : false);
}
eXorithm – Execute Algorithm: View / Run Algorithm merge_sort
function merge_sort ($array
{
  if (count$array) <= 1) Â
    return $array; Â
 Â
  // sort each half
  $mid = floorcount$array)/2);
  $left = merge_sortarray_slice$array, 0, $mid)); Â
  $right = merge_sortarray_slice$array, $mid)); Â
 Â
  // merge the arrays
  $array = array();
  while (count$left)>0 && count$right)>0) { Â
    if ($left[0] <= $right[0]) { Â
      array_push$array, array_shift$left)); Â
    } else { Â
      array_push$array, array_shift$right)); Â
    } Â
  } Â
  $array = array_merge$array, $left, $right); Â
 Â
  return $array
}Â
eXorithm – Execute Algorithm: View / Run Algorithm highlight
function highlight ($text, $phrase, $highlighter
{
  if (empty$phrase)) {
    return $text
  }
 Â
  if (is_array$phrase)) {
    $replace = array();
    $with = array();
   Â
    foreach ($phrase as $key => $value) {
      $key = $value
      $value = $highlighter
      $key = '([s])(' . $key . ')([s.,!?<])'
      $replace[] = '|' . $key . '|ix'
      $with[] = empty$value) ? $highlighter : $value
    }
    return preg_replace$replace, $with, $text);
  } else {
    $phrase = '([s])(' . $phrase . ')([s])'
   Â
    return preg_replace'|'$phrase'|i', $highlighter, $text);
  }
}Â
eXorithm – Execute Algorithm: View / Run Algorithm lathe_shape
function lathe_shape ($equation, $start_x, $end_x, $detail, $degree_x, $degree_y, $degree_z, $face_color, $vertex_color, $scale, $image_size
{
$vdist = 20;
$dist = 20;
$sides = lathe_polygons$equation, $start_x, $end_x, $detail);
// project each of the polygons
for ($i=0; $icount$sides); $i++) {
$points[] = project_polygon$sides$i], $degree_x, $degree_y, $degree_z, ($end_x$start_x)/2, 0, 0, 20, 20, true);
}
// scale the image somewhat
$scale = $image_size$scale/($end_x$start_x);
return render_polygons$points, $vertex_color, $face_color, false, false, $image_size, $scale);
}
eXorithm – Execute Algorithm: View / Run Algorithm kmp_search
function kmp_search ($x, $y
{
// see http://www-igm.univ-mlv.fr/~lecroq/string/node8.html
// set-up phase
$i = 0;
$j = -1;
$kmpNext = array();
while ($i < $m) {
while ($j > -1 && $x[i] != $x[j])
$j = $kmpNext$j];
$i++;
$j++;
if ($x$i] == $x$j])
$kmpNext$i] = $kmpNext$j];
else
$kmpNext$i] = $j
}
// search phase
$i = 0;
$j = 0;
$m = strlen$x);
$n = strlen$y);
$results = array();
while ($j < $n) {
while ($i > -1 && $x$i] != $y$j])
$i = $kmpNext$i];
$i++;
$j++;
if ($i >= $m) {
$results[] = $j$i+1;
$i = $kmpNext$i];
}
}
return $results
}