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: Polygons
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 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 projectile_distance
function projectile_distance ($inital_velocity, $angle, $initial_height, $g
{
$angle = deg2rad$angle);
$distance = ($inital_velocitycos$angle)/$g
* ($inital_velocitysin$angle)
+ sqrtpow$inital_velocitysin$angle),2)+2*$g$initial_height));
return $distance
}
eXorithm – Execute Algorithm: View / Run Algorithm pluralize
function pluralize ($word, $exceptions
{
if (array_key_exists$word, $exceptions))
return $exceptions$word];
if (substr_compare$word, 'y', -1, 1)==0) {
if (in_arraysubstr$word, -2, 1), array'a''e''i''o''u''y'))===false)
return substr$word, 0, -1).'ies'
else
return $word's'
}
if (substr_compare$word, 'o', -1, 1)==0) {
if (in_arraysubstr$word, -2, 1), array'a''e''i''o''u'))===false)
return $word'es'
else
return $word's'
}
if (substr_compare$word, 's', -1, 1)==0)
return $word'es'
if (substr_compare$word, 'x', -1, 1)==0)
return $word'es'
if (substr_compare$word, 'z', -1, 1)==0)
return $word'es'
if (substr_compare$word, 'ch', -2, 2)==0)
return $word'es'
if (substr_compare$word, 'sh', -2, 2)==0)
return $word'es'
return $word's'
}
eXorithm – Execute Algorithm: View / Run Algorithm ordinal
function ordinal ($num
{
// get the last two digits
$last = $num % 100;
// get last digit unless the number is in the teens
$last = ($last < 20) ? $last : $num % 10;
$ord = ($last==1) ? 'st' : (($last==2) ? 'nd' : (($last==3) ? 'rd' : 'th'));
return $num$ord
}
eXorithm – Execute Algorithm: View / Run Algorithm reverse_factorial
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;
}
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 radioactive_decay
function radioactive_decay ($half_life, $time
{
$lambda = log(2)/$half_life
return 100 - 100 * powexp(1), -1 * $lambda * $time);
}