How to calculate max. is the number of combinations possible?

Possible duplicates:
Show possible combinations of strings
that will take numbers or words and find all possible combinations

If I have 3 lines, for example:

"abc def xyz"

And I want to find the maximum number of combinations that I can generate by rearranging these lines, for example:

  • abc xyz def
  • def xyz abc
  • xyz abc def

etc .. What is the formula / algorithm for calculating this?

+3
source share
4 answers

This is not a combination, but a permutation. Algorithm n! where n is the number of elements.

Why?

3 , , , ( ) .

3 * 2 * 1 = 3!= 6

,

, 3- ,

3 * 3 * 3 = 3 ^ 3 = 27

n ^ k - n - , k - ""

:

function fact($n)
{
  if ($n == 0)
  {
    return 1;
  }
  else
  {
    return $n * fact($n - 1);
  }
}

+8

n! .

9

9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 = 362880

+1

. O'Reilley google. , .

, 100%, , ( O'Reilley, fyi):

<?php
function pc_permute($items, $perms = array( )) {
    if (empty($items)) { 
        print join(' ', $perms) . "\n";
    }  else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
             $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             pc_permute($newitems, $newperms);
         }
    }
}

pc_permute(array('abc', 'xyz', 'def', 'hij'));
?>

, , lurkers:) . , n!

0

3 * 2 * 1 = 6 !

3 = 6 ..... 4 = 24 ..... ..

0

Source: https://habr.com/ru/post/1758433/


All Articles