SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    May 2001
    Location
    hong kong
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 == 
       { 
          return 
    1;
       }
       else 
       {
          return 
    $num factorial($num 1);
       }

    }

  4. #4
    SitePoint Addict
    Join Date
    May 2001
    Location
    hong kong
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  6. #6
    SitePoint Addict
    Join Date
    May 2001
    Location
    hong kong
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very very grateful for the help.....


Bookmarks

Posting Permissions

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