function prime_factors ($number
{
$factors = array();
while$number > 1)
{
$prime = 2;
while$number % $prime != 0)
{
$prime = next_prime$prime);
}
if$prime > floor$number/2))
{
$factors[] = $number
break
}
$factors[] = $prime
$number = $number$prime
}
return $factors
}
Tag: Prime
eXorithm – Execute Algorithm: View / Run Algorithm pie_chart
function pie_chart ($data
{
$size=200;
$centerx = 100;
$centery = 100;
$circle_diameter = 180;
$image = image_create_alpha$size, $size);
$blackimagecolorallocatealpha$image, 0, 0, 0, 0);
$total = array_sum$data);
$n = 0;
$start = 0;
foreach ($data as $key => $value) {
$length = 360 * $value$total
$color = allocate_color$image, generate_graph_color$n++));
imagefilledarc$image, $centerx, $centery, $circle_diameter, $circle_diameter, round$start), round$start$length), $color, IMG_ARC_PIE);
$start += $length
}
imageellipse$image, $centerx, $centery, $circle_diameter, $circle_diameter, $black);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm moon_cycle
function moon_cycle ($date
{
$d = getdate$date);
$year = $d'year'];
$month = $d'mon'];
$day = $d'mday'];
$r = $year % 100;
$r %= 19;
if ($r>9)
$r -= 19;
$r = (($r * 11) % 30) + $month + $day
if ($month<3)
$r += 2;
$r -= (($year<2000) ? 4 : 8.3);
$r = floor$r+0.5)%30;
return ($r < 0) ? $r+30 : $r
}
eXorithm – Execute Algorithm: View / Run Algorithm multicolumn
function multicolumn ($items, $type, $rows, $columns, $table_attributes, $td_attributes, $empty_attributes
{
$return = ''
$count = count$items);
// compute number of columns and rows
if (($rows=='*') && ($columns=='*')) {
$rows = roundsqrt$count));
$columns = ceil$count$rows);
} else {
if ($rows=='*'
$rows = ceil$count$columns);
else if ($columns=='*'
$columns = ceil$count$rows);
}
if ($count>0) {
$return .= "<table $table_attributes>"
$column = 0;
$row = 0;
$total = $rows * $columns
for ($i=0;$i$total$i++) {
if$column == 0)
$return .= '<tr>'
if ($type=='horizontal'
$spot = $i
else
$spot = $row$column$rows
if ($spot$count
$return .= "<td $td_attributes>$items[$spot]</td>"
else
$return .= "<td $empty_attributes></td>"
$column++;
if ($column==$columns) {
$return .= '</tr>'
$column = 0;
$row++;
}
}
$return .= '</table>'
}
return $return
}
eXorithm – Execute Algorithm: View / Run Algorithm list_prime
function list_prime ($min, $max, $return_string
{
$count = $min
$numbert = 2;
$return = array();
while ($count < $max ) {
$div_count=0;
for ( $i=1;$i<=$number$i++)
if (($number$i)==0)
$div_count++;
if ($div_count<3) {
$return[] = $number
$count += 1;
}
$number += 1;
}
unset$return[0]);
if$return_string
$return = implode", ", $return);
return $return
}
eXorithm – Execute Algorithm: View / Run Algorithm overlay_image
function overlay_image ($base, $image
{
$h = imagesy$image);
$w = imagesx$image);
imagealphablending$base, true);
imagesavealpha$base, true);
imagecopy$base, $image, 0, 0, 0, 0, $w, $h);
return $base
}
eXorithm – Execute Algorithm: View / Run Algorithm photobucket
function photobucket ($url
{
$str = file_get_contents$url);
preg_match_all'/<img[^>]+>/i'$str, $result);
$strPics = ""
foreach( $result as $img_tag) {
foreach( $img_tag as $img) {
if( !strpos$img, 'class="under off"') ) continue
preg_match'/< *img[^>]*src *= *["']?([^"']*)/i', $img, $imgURLs);
$imgURL = str_replace"/th_", "/", $imgURLs[1]);
$strPics .= $imgURL . "n"
}
}
return $strPics
}
eXorithm – Execute Algorithm: View / Run Algorithm php_manual
function php_manual ($function_name
{
$function_name = strtolowerstr_replace'_', '-', $function_name));
return "http://www.php.net/manual/en/function."$function_name".php"
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_hyphenate
function isbn_hyphenate ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric chars
// strip prefix from ISBN13
if (strlen$isbn)==13) {
$prefix = substr$isbn,0,3).'-'
$isbn = substr$isbn,3);
} else if (strlen$isbn)==10) {
$prefix = ''
} else {
return ''
}
$unknown = substr$isbn,1,4)+0;
$publisher = ''
$unit = ''
if (($isbn[0] == '0') || ($isbn[0] == '3') || ($isbn[0] == '4'))
// english region 0
// german region 3
// japan region 4
{
if$unknown<=1999)
{
$publisher= substr$isbn,1,2);
$unit= substr$isbn,3,6);
}
elseif$unknown>=2000 && $unknown<=6999)
{
$publisher= substr$isbn,1,3);
$unit= substr$isbn,4,5);
}
elseif$unknown>=7000 && $unknown<=8499)
{
$publisher= substr$isbn,1,4);
$unit= substr$isbn,5,4);
}
elseif$unknown>=8500 && $unknown<=8999)
{
$publisher= substr$isbn,1,5);
$unit= substr$isbn,6,3);
}
elseif$unknown>=9000 && $unknown<=9499)
{
$publisher= substr$isbn,1,6);
$unit= substr$isbn,7,2);
}
elseif$unknown>=9500)
{
$publisher= substr$isbn,1,7);
$unit= $isbn[8];
}
return $prefix$isbn[0]."-"$publisher"-"$unit"-"$isbn[9];
}
else if ($isbn[0] == '1'
// english region 1
{
if$unknown<=999)
{
$publisher= substr$isbn,1,2);
$unit= substr$isbn,3,6);
}
elseif$unknown>=1000 && $unknown<=3999)
{
$publisher= substr$isbn,1,3);
$unit= substr$isbn,4,5);
}
elseif$unknown>=4000 && $unknown<=5499)
{
$publisher= substr$isbn,1,4);
$unit= substr$isbn,5,4);
}
elseif$unknown>=5500 && $unknown<=8697)
{
$publisher= substr$isbn,1,5);
$unit= substr$isbn,6,3);
}
elseif$unknown>=8698 && $unknown<=9989)
{
$publisher= substr$isbn,1,6);
$unit= substr$isbn,7,2);
}
elseif$unknown>=9990)
{
$publisher= substr$isbn,1,7);
$unit= $isbn[8];
}
return $prefix$isbn[0]."-"$publisher"-"$unit"-"$isbn[9];
}
else
// other regions are not fully supported
{
return $prefixsubstr$isbn,0,9)."-"$isbn[9];
}
}
eXorithm – Execute Algorithm: View / Run Algorithm make_change
function make_change ($amount, $coins
{
$coin_count = count$coins);
$table = array();
for ($i = -1; $i <= $amount; $i++) {
for$j = -1; $j <= $coin_count; $j++) {
// Rules
// 1: table[0,0] or table[0,x] = 1
// 2: talbe[i <= -1, x] = 0
// 3: table[x, j <= -1] = 0
$total = 0;
// first sub-problem
// count(n, m-1)
$n = $i
$m = $j-1;
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1))
$total += 0;
else
$total += $table$n][$m];
// second sub-problem
// count(n-S[m], m)
if (($j-1) <= -1)
$total += 0;
else {
$n = $i - $coins$j - 1];
$m = $j
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1)) // rule 3
$total += 0;
else
$total += $table$n][$m];
}
$table$i][$j] = $total
}
}
return $table$i-1][$j-1];
}