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];
}
Category: Algorithm
eXorithm – Execute Algorithm: Embed Algorithm check_domain
Embed This Algorithm
This page will help you embed the algorithm check_domain on a page on your own website. Just configure the inputs, then click the generate button to get a snippet of code you can paste onto your site. You have two options.
- You can embed the entire form. Users will be able to enter their own arguments, and will need to press the run button to execute the algorithm.
- You can add only the output of the algorithm to your website. There will be no argument inputs or run button.
eXorithm – Execute Algorithm: View / Run Algorithm array_rsearch
function array_rsearch ($search, $array, $strict
{
$array = array_reverse$array, true);
foreach ($array as $key => $value){
if ($strict) {
if ($value === $search
return $key
} else {
if ($value == $search
return $key
}
}
return false;
}
Fibonacci Binet
<?php /** * fibonacci_binet * * Simple example of Fibonacci series algorithm using Binet * * @version 0.3 * @author Contributors at eXorithm * @link /algorithm/view/fibonacci_binet Listing at eXorithm * @link /algorithm/history/fibonacci_binet History at eXorithm * @license /home/show/license * * @param number $n * @return mixed */ function fibonacci_binet($n=13) { $phi = (1 + sqrt(5)) / 2; $u = (pow($phi, $n) - pow(1 - $phi, $n)) / sqrt(5); return $u; } ?>
eXorithm – Execute Algorithm: View / Run Algorithm create_gradient
function create_gradient ($start_color, $end_color, $size, $thickness, $orientation
{
if ($orientation=="vertical") {
$imgimagecreatetruecolor$thickness$size);
} else {
$imgimagecreatetruecolor$size$thickness);
}
$start_r = hexdecsubstr$start_color, 0, 2));
$start_g = hexdecsubstr$start_color, 2, 2));
$start_b = hexdecsubstr$start_color, 4, 2));
$end_r = hexdecsubstr$end_color, 0, 2));
$end_g = hexdecsubstr$end_color, 2, 2));
$end_b = hexdecsubstr$end_color, 4, 2));
for ($i=0;$i$size$i++) {
$red = round$start_r - ($start_r$end_r) * ($i / ($size-1)));
$green = round$start_g - ($start_g$end_g) * ($i / ($size-1)));
$blue = round$start_b - ($start_b$end_b) * ($i / ($size-1)));
$color = imagecolorallocate$img, $red, $green, $blue);
if ($orientation=="vertical") {
for ($k=0;$k$thickness$k++)
imagesetpixel$img, $k, $i, $color);
} else {
for ($k=0;$k$thickness$k++)
imagesetpixel$img, $i, $k, $color);
}
}
return $img
}
eXorithm – Execute Algorithm: View / Run Algorithm insertion_sort
function insertion_sort ($array
{
for$ii=1; $iicount$array); $ii++) {
$value = $array$ii];
$done = false;
$jj = $ii-1;
do {
if ($array$jj] > $value) {
$array$jj+1] = $array$jj];
$jj = $jj-1;
if ($jj<0)
$done = true;
} else {
$done = true;
}
} while (!$done);
$array$jj+1] = $value;
}
return $array
}
eXorithm – Execute Algorithm: View / Run Algorithm calculate_median
function calculate_median ($numbers
{
sort$numbers);
$c = count$numbers) - 1;
if ($c%2) {
$b = round$c/2);
$a = $b-1;
return ($numbers$b] + $numbers$a]) / 2 ;
} else {
return $numbers[($c/2)];
}
}