PDA

View Full Version : [High School] Algorithm in PHP

lord_carbo
03-1-2008, 12:55 PM
(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.

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;
}

argo15
03-2-2008, 08:26 AM
hmm. i don't know php but it looks similar to c, can't you debug through it?
And can you explain your variables.

lord_carbo
03-5-2008, 09:42 PM
The explanation for the variables in the function solle() are at the top of my post. The variables for the binomial coefficient and factorial functions are mostly irrelevant, and are easy to read in the first place.

I'm not sure how to debug codes, much less how to debug this one. It doesn't come up with "syntax error on line blah blah." I'm trying to get it to return certain numbers and the computer doesn't know my intentions. For example, solle(2,7,1,6) should return 6 (think of a die problem: you roll two dice, and there is a 6/36 chance that the sum of the two rolls will be 7). But that doesn't happen.

Edit: Got it kind of working, but there's a problem that occurs when it subtracts the intersection. First of all, I was stupid and assumed that ^ was an operator. I should probably kill myself, but I won't.

Here's the new 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;\$k>=\$f; \$f++) {
if((\$n -((\$d + 1) * \$f))>0&&(\$d + \$f)!=0){
\$e = -1;
for(\$q=0;\$f>=\$q; \$q++){
\$e = \$e * -1;
}
\$z = \$z + (\$e * bicoefficient(\$k,\$f) * bicoefficient((\$n + \$k -((\$d + 1) * \$f) -1),(\$n -((\$d + 1) * \$f))));
}
}
return \$z;
}

solle(2,5,1,6) returns 4 (good)
solle(2,6,1,6) returns 5 (good)
solle(2,7,1,6) returns 6 (good)
solle(2,8,1,6) returns 3 (should be 5)
solle(2,9,1,6) returns 2 (should be 4)
solle(2,10,1,6) returns 1 (should be 3)
solle(2,11,1,6) returns 0 (should be 2)
solle(2,12,1,6) returns -1 (should be 1)
solle(2,13,1,6) returns 0 (good)
solle(2,14,1,6) returns 0 (good)

Edit 2: GOT IT. \$d = \$d - (\$k * \$c) should have been \$d = \$d - \$c. I'm done. This can be locked I guess.