1. ## factorial calcultion

This is the way of a factorial calculation from a math. of permutation.

\$a=30;
\$b=6;
\$result=\$a*(\$a-(\$b+1))*(\$a-(\$b+2))*(\$a-(\$b+3))*(\$a-(\$b+4))*(\$a-(\$b+5))

Simple question ..
Just how to make it simpler or making use of logical operater

Thanks...

2. Not had time to test this so it may or may not work:
PHP Code:
``` \$a_total = 1; for( \$a = 30; \$a > 0; \$a-- ) {     \$a_total *= \$a; } \$b_total = 1; for( \$b = 6; \$b > 0; \$b-- ) {     \$b_total *= \$b; } \$total = \$a - \$b;  ```

3. Karl, a quick desk check of your code reveals that

\$total = \$a - \$b;

must be zero!

That said, being a maths imbicile, I am not sure what the algorithm that uatt is looking for is supposed to do

However, calculating a factorial is a traditional example of how to apply recursion in programming. (Note: this is not best practice, use iteration in place of recursion in practice - this is just an "academic" use of recursion)

PHP Code:
``` function factorial(\$num)  {    if ( \$num == 0 )     {        return 1;    }    else     {       return \$num * factorial(\$num - 1);    } }  ```
}

4. This is not right as what the needs are
\$result=\$a*(\$a-(\$b+1))*(\$a-(\$b+2))*(\$a-(\$b+3))*(\$a-(\$b+4))*(\$a-(\$b+5))

The \$b merge with \$a So it is not simply
\$result = \$a - \$b;

I am thinking about two loops(one nest in another) but stucking on how to do it..

5. Ok, correction now:
PHP Code:
``` <?php \$a = 30; \$b = 6; \$res = \$a; for( \$loop = 1; \$loop < 6; \$loop ++ ) {     \$res *= (\$a-(\$b+\$loop)); } ?> ```
That produces the same result for \$a = 30 and \$b = 6 as your example above does.

6. Very very grateful for the help.....

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•