function number_string ($number
{
$names = array
0=>"zero", 1=>"one", 2=>"two", 3=>"three", 4=>"four"
5=>"five", 6=>"six", 7=>"seven", 8=>"eight", 9=>"nine"
10=>"ten", 11=>"eleven", 12=>"twelve", 13=>"thirteen", 14=>"fourteen"
15=>"fifteen", 16=>"sixteen", 17=>"seventeen", 18=>"eighteen", 19=>"nineteen"
20=>"twenty", 30=>"thirty", 40=>"forty", 50=>"fifty"
60=>"sixty", 70=>"seventy", 80=>"eighty", 90=>"ninety"
);
$triplets = array
100=>"hundred"
1000=>"thousand"
1000000=>"million"
1000000000=>"billion"
1000000000000=>"trillion"
1000000000000000=>"quadrillion"
1000000000000000000=>"quintillion"
1000000000000000000000=>"sextillion"
1000000000000000000000000=>"septillion"
1000000000000000000000000000=>"octillion"
1000000000000000000000000000000=>"nonillion"
);
krsort$names);
krsort$triplets);
if (abs$number) > PHP_INT_MAX)
throw new Exception'Maximum integer size for this version of PHP is '.PHP_INT_MAX);
$prefix = ''
$str = ''
if ($number<0) {
$prefix = 'negative '
$number = $number*-1;
}
$remainder = 0;
if ($number>=100) {
foreach ($triplets as $num=>$name) {
if ($num<=$number) {
$str = $name
$remainder = $number % $num
$number = floor$number$num);
break
}
}
// how many of the triplets?
$str = number_string$number).' '$str
if ($remainder<100) $separator = ' and '
else $separator = ', '
} else {
foreach ($names as $num=>$name) {
if ($num<=$number) {
$str = $name
$remainder = $number % $num
break
}
}
$separator = '-'
}
// remainder to tack on?
if ($remainder>0) {
$str = $str$separatornumber_string$remainder);
}
return $prefix$str
}
Tag: Prime
eXorithm – Execute Algorithm: View / Run Algorithm locate_country
function locate_country ($country
{
$data = file_get_contents"http://maps.google.com/maps/geo?output=csv&q="urlencode$country));
$arr = explode",", $data);
if (count$arr)>=4) {
if ($arr[0]==200) {
return array'latitude'=>$arr[2], 'longitude'=>$arr[3]);
} else {
throw new Exception'Country could not be geocoded');
}
} else {
throw new Exception'Country could not be geocoded');
}
}
eXorithm – Execute Algorithm: View / Run Algorithm raise_polygon
function raise_polygon ($points, $height, $direction, $color
{
// check points
if ((count$points)==0) || ((count$points)%2)!=0)) {
throw new Exception'The points must be an array with an even number of integers.');
}
// determine the extent of the polygon
$maxx = $points[0];
$maxy = $points[1];
$minx = $points[0];
$miny = $points[1];
for ($i=2; $icount$points); $i$i+2) {
$x = $points$i];
$y = $points$i+1];
if ($x$maxx) $maxx = $x
if ($y$maxy) $maxy = $y
if ($x$minx) $minx = $x
if ($y$miny) $miny = $y
}
// determine the extent of the projection
$proj_length = roundsqrt$height$height/2));
switch ($direction) {
case 'nw'
$projx = $proj_length*-1;
$projy = $proj_length*-1;
break
case 'ne'
$projx = $proj_length
$projy = $proj_length*-1;
break
case 'sw'
$projx = $proj_length*-1;
$projy = $proj_length
break
default
$projx = $proj_length
$projy = $proj_length
}
// determine where the polygon should be positioned
for ($i=0; $icount$points); $i$i+2) {
$points$i] = $points$i]-$minx+1;
$points$i+1] = $points$i+1]-$miny+1;
if ($projx<0) $points$i] = $points$i] - $projx
if ($projy<0) $points$i+1] = $points$i+1] - $projy
}
// blank image
$image = image_create_alpha$maxx$minx$proj_length+2, $maxy$miny$proj_length+2);
// create the colors
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image, $r, $g, $b);
// draw the first polygon
imagepolygon$image, $points, count$points)/2, $color);
// draw the lines + modify the points
for ($i=0; $icount$points); $i$i+2) {
imageline$image, $points$i], $points$i+1], $points$i]+$projx, $points$i+1]+$projy, $color);
$points$i] = $points$i]+$projx
$points$i+1] = $points$i+1]+$projy
}
// draw the second polygon
imagepolygon$image, $points, count$points)/2, $color);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm invert_image
function invert_image ($image
{
$image_width = imagesx$image);
$image_height = imagesy$image);
// if the image is not true color, make it so
if (!imageistruecolor$image)) {
$image2 = imagecreatetruecolor$image_width, $image_height);
imagecopy$image2$image,0,0,0,0,$image_width$image_height);
$image = $image2
}
// loop through all the pixels
for ($h = 0; $h < $image_height; $h++) {
for ($w = 0; $w < $image_width; $w++) {
// get the color at this pixel
$color = imagecolorsforindex$image, imagecolorat$image, $w, $h));
// invert the color
$color'red'] = 255 - $color'red'];
$color'green'] = 255 - $color'green'];
$color'blue'] = 255 - $color'blue'];
// create the new color
$new_color = imagecolorallocate$image, $color'red'], $color'green'], $color'blue']);
// set the color
imagesetpixel$image, $w, $h, $new_color);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm primes
function primes ($number_of_primes
{
$n = $number_of_primes
$primes = array();
for$i = 2; ; $i++)
{
$is_prime = true;
for$j = 2; $j < $i; $j++)
{
if(($i % $j) == 0)
{
$is_prime = false;
break
}
if$j > floor$i/2)) break
}
if ($is_prime
{
$primes[] = $i
$n--;
}
if ($n == 0) break
}
return $primes
}
eXorithm – Execute Algorithm: View / Run Algorithm pi_digits
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);
}
}
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
}
eXorithm – Execute Algorithm: View / Run Algorithm magic8ball
function magic8ball ()
{
$answers =array'It is certain', 'It is decidedly so', 'Without a doubt'
'Yes – definitely', 'You may rely on it', 'As I see it, yes'
'Most likely', 'Outlook good', 'Signs point to yes', 'Yes'
'Reply hazy, try again', 'Ask again later'
'Better not tell you now', 'Cannot predict now'
'Concentrate and ask again', 'Don't bet on it'
'My reply is no', 'My sources say no', 'Outlook not so good'
'Very doubtful' );
$index = rand(0, count$answers));
return ($answers$index]);
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_validate
function isbn_validate ($isbn, $type
{
$okay = false;
$isbn = str_replacearray'-'' '), '', $isbn);
// check ISBN 13
if ($type!=2)
{
if (strlen$isbn)==13)
{
$sum=0;
$charsokay=true;
for ($i=0;$i<12;$i++)
{
if (($i%2)==1)
$weight=3;
else
$weight=1;
if (is_numeric$isbn$i]))
$sum += $weight$isbn$i];
else
$charsokay=false;
}
if ($charsokay
if ($sum>0)
if ((10-($sum % 10)) % 10==$isbn[12])
$okay = true;
}
}
// check ISBN 10
if ($type!=1)
{
if (strlen$isbn)==10)
{
$sum=0;
$charsokay=true;
for ($i=0;$i<10;$i++)
{
if (is_numeric$isbn$i]))
$sum += ($i+1)*$isbn$i];
else
{
if ((strtolower$isbn$i])=='x') && ($i==9))
$sum += 100;
else
$charsokay=false;
}
}
if ($charsokay
if ($sum>0)
if (($sum % 11)==0)
$okay = true;
}
}
return $okay
}