(Edit: actually this is just an "information" thread.) (Lol Carbo reads stickies well)
I'm trying to write a script in PHP that finds the number of solutions of a linear equation u1 + u2 ... uk = n, when u1, u2 ... uk ∈ {c, c+1, ... d}. And I can't get it to work.
The factorial and binomial coefficient functions work alright. And factorial($x) returns 0 when $x < 0.
I'm trying to write a script in PHP that finds the number of solutions of a linear equation u1 + u2 ... uk = n, when u1, u2 ... uk ∈ {c, c+1, ... d}. And I can't get it to work.
The factorial and binomial coefficient functions work alright. And factorial($x) returns 0 when $x < 0.
Code:
function factorial($x) {
$y = 1;
if($x>0) {
for ($a=2; $a <= $x; $a++) {
$y = ($y*$a);
}
}
elseif($x==0){
$y = 1;
}
else {
$y = 0;
}
return $y;
}
function bicoefficient($r, $c) {
$a = factorial($r)/(factorial($r-$c)*factorial($c));
return $a;
}
function solle($k, $n, $c, $d) {
$n = $n - ($k * $c);
if($d > 0) {
$d = $d- ($k * $c);
}
else {
$d = -1;
}
$z = 0;
for ($f=0;($d + $f)!=0&&($n -(($d + 1) * $f))>0&&$k>=$f; $f++) {
$z = $z + ((-1) ^ ($f) * bicoefficient($k,$f) * bicoefficient(($n + $k -(($d + 1) * $f) -1),($n -(($d + 1) * $f))));
}
return $z;
}
Comment