Combinatorics algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • all_my_trash@hotmail.com

    Combinatorics algorithm

    Hello!

    I'm trying to write an algorithm which can fill an array with all the
    possible combinations of r out of n, without repetition (maximum
    combinations = n!/(r!(n-r)!))

    However, my mathematical skills are insufficient - does anybody have
    such an algorithm for sharing?

    Regards,
    OMykle

  • Craig Storey

    #2
    Re: Combinatorics algorithm

    all_my_trash@ho tmail.com wrote:
    Hello!
    >
    I'm trying to write an algorithm which can fill an array with all the
    possible combinations of r out of n, without repetition (maximum
    combinations = n!/(r!(n-r)!))
    >
    However, my mathematical skills are insufficient - does anybody have
    such an algorithm for sharing?
    >
    Regards,
    OMykle
    >
    Hopefully this isn't for homework...but if it is then you are copying
    mine. :) This is untested code, but mathematically correct. For bonus
    marks you can figure out how to do this recursively.

    function Factorial($x)
    {
    if ($x<1) {echo "Factorial( ) Error: Number too small!";)
    $ans = 1;
    for ($xx=2; $xx>=$x; $xx++)
    {
    $ans = $ans * $xx;
    }
    return($ans);
    }

    function Permutation($se lectcount,$avai lablecount)
    {
    $ans = Factorial($avai lablecount) /
    Factorial($avai lablecount-$selectcount);
    return ($ans);
    }

    function Combination($se lectcount,$avai lablecount)
    {
    $ans = Factorial($avai lablecount) / (
    Factorial($avai lablecount-$selectcount) * Factorial($sele ctcount) );
    return ($ans);
    }

    Comment

    • zman

      #3
      Re: Combinatorics algorithm


      all_my_trash@ho tmail.com wrote:
      Hello!
      >
      I'm trying to write an algorithm which can fill an array with all the
      possible combinations of r out of n, without repetition (maximum
      combinations = n!/(r!(n-r)!))
      >
      Java code is given here (which will give you the psuedo code for php
      code):





      zafar
      ------------------------------



      Comment

      Working...