function isbn_validator ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric or X chars
$casestrlen$isbn);
switch ($case
{
case 10:
if(!isbn_validate$isbn, 2))
return 0; //Failure - not valid ISBN
else
$isbnconv_isbn$isbn);
break
case 13:
$isbnpreg_replace'/[^d]/'''$isbn); //remove all non-numeric chars
if(!isbn_validate$isbn, 1))
return 0; //Failure - not valid ISBN
break
default
return 0;
}
$isbnisbn_hyphenate$isbn);
return $isbn
}
Tag: Prime
eXorithm – Execute Algorithm: View / Run Algorithm morse
function morse ($data, $direction
{
$codes = array
'a' => '.-'
'b' => '-...'
'c' => '-.-.'
'd' => '-...'
'e' => '.'
'f' => '..-.'
'g' => '--.'
'h' => '....'
'i' => '..'
'j' => '.---'
'k' => '-.-'
'l' => '.-..'
'm' => '--'
'n' => '-.'
'o' => '---'
'p' => '.--.'
'q' => '--.-'
'r' => '.-.'
's' => '...'
't' => '-'
'u' => '..-'
'v' => '...-'
'w' => '.--'
'x' => '-..-'
'y' => '-.--'
'z' => '--..'
'0' => '-----'
'1' => '.----'
'2' => '..---'
'3' => '...--'
'4' => '....-'
'5' => '.....'
'6' => '-....'
'7' => '--...'
'8' => '---..'
'9' => '----.'
',' => '--..--'
'.' => '.-.-.-'
'?' => '..--..'
);
$output = ''
if ($direction=='text') {
$codes = array_flip$codes);
$elements = explode' ', $data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element];
}
} else {
$data = strtolower$data);
$elements = str_split$data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element].' '
}
$output = trim$output);
}
return $output
}
eXorithm – Execute Algorithm: View / Run Algorithm number2roman
function number2roman ($num
{
// http://www.go4expert.com/forums/showthread.php?t=4948
// Make sure that we only use the integer portion of the value
$n = intval$num);
$result = ''
// Declare a lookup array that we will use to traverse the number:
$lookup = array'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
foreach ($lookup as $roman => $value)
{
// Determine the number of matches
$matches = intval$n / $value);
// Store that many characters
$result .= str_repeat$roman, $matches);
// Substract that from the number
$n = $n % $value
}
// The Roman numeral should be built, return it
return $result
}
eXorithm – Execute Algorithm: View / Run Algorithm lathe_polygons
function lathe_polygons ($equation, $start_x, $end_x, $detail
{
$degrees = 360/$detail
$step = ($end_x$start_x)/$detail
$max_y = 0;
for ($ii=0;$ii<=$detail$ii++) {
$x = $start_x+($ii$step);
$y0 = evaluate_for_x$equation, $x);
if (!is_nan$y0) && !is_infinite$y0)) {
if ($y0 > $max_y) $max_y = $y0
for ($jj=0;$jj$detail$jj++) {
$y = $y0 * sindeg2rad$degrees$jj));
$z = $y0 * cosdeg2rad$degrees$jj));
// the top of the shape
/*if ($ii==0) {
$sides[-1][] = $x;
$sides[-1][] = $y;
$sides[-1][] = $z;
}*/
// the bottom of the shape
/*if ($ii==$detail) {
$sides[-2][] = $x;
$sides[-2][] = $y;
$sides[-2][] = $z;
}*/
// the sides of the shape
if ($ii$detail) {
$poly = $ii$detail$jj+1;
$sides$poly][6] = $x
$sides$poly][7] = $y
$sides$poly][8] = $z
$poly = $ii$detail+(($jj$detail-1) % $detail)+1;
$sides$poly][9] = $x
$sides$poly][10] = $y
$sides$poly][11] = $z
}
// the sides of the shape
if ($ii>0) {
$poly = ($ii-1)*$detail$jj+1;
$sides$poly][3] = $x
$sides$poly][4] = $y
$sides$poly][5] = $z
$poly = ($ii-1)*$detail+(($jj$detail-1) % $detail)+1;
$sides$poly][0] = $x
$sides$poly][1] = $y
$sides$poly][2] = $z
}
}
}
}
$sides = array_values$sides);
return $sides
}
eXorithm – Execute Algorithm: View / Run Algorithm markup_urls
function markup_urls ($text
{
// split the text into words
$words = preg_split'/([snr]+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$text = ""
// iterate through the words
foreach$words as $word) {
// chopword = the portion of the word that will be replaced
$chopword = $word
$chopword = preg_replace'/^[^A-Za-z0-9]*/', '', $chopword);
if ($chopword <> '') {
// linkword = the text that will replace chopword in the word
$linkword''
// does it start with http://abc. ?
if (preg_match'/^(http://)[a-zA-Z0-9_]{2,}.*/', $chopword)) {
$chopword = preg_replace'/[^A-Za-z0-9/]*$/', '', $chopword);
$linkword = '<a href="'$chopword'" target="blank">'$chopword'</a>'
// does it equal abc.def.ghi ?
} else if (preg_match'/^[a-zA-Z]{2,}.([a-zA-Z0-9_]+.)+[a-zA-Z]{2,}(/.*)?/', $chopword)) {
$chopword = preg_replace'/[^A-Za-z0-9/]*$/', '', $chopword);
$linkword = '<a href="http://'$chopword'" target="blank">'$chopword'</a>'
// does it start with [email protected] ?
} else if (preg_match'/^[a-zA-Z0-9_.]+@([a-zA-Z0-9_]{2,}.)+[a-zA-Z]{2,}.*/', $chopword)) {
$chopword = preg_replace'/[^A-Za-z0-9]*$/', '', $chopword);
$linkword = '<a href="mailto:'$chopword'">'$chopword'</a>'
}
// replace chopword with linkword in word (if linkword was set)
if ($linkword <> '') {
$word = str_replace$chopword, $linkword, $word);
}
}
// append the word
$text = $text$word
}
return $text
}
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
}