php - Permutations - all possible sets of numbers -


i have numbers, 0 8. in result, possible sets of numbers, each set should use numbers, each number can occur once in set.

i see solution made in php print out result. or, @ least, refreshment in theory of combinatorics, have long forgotten it. formula calculate how many permutations there be?

example sets:

  • 0-1-2-3-4-5-6-7-8
  • 0-1-2-3-4-5-6-8-7
  • 0-1-2-3-4-5-8-6-7
  • 0-1-2-3-4-8-5-6-7
  • 0-1-2-3-8-4-5-6-7
  • 0-1-2-8-3-4-5-6-7
  • and on...

you're looking permutations formula:

npk = n!/(n-k)! 

in case, have 9 entries , want choose of them, that's 9p9 = 9! = 362880

you can find php algorithm permutate in recipe 4.26 of o'reilly's "php cookbook".

pc_permute(array(0, 1, 2, 3, 4, 5, 7, 8)); 

copied in o'reilly:

function pc_permute($items, $perms = array( )) {     if (empty($items)) {          print join(' ', $perms) . "\n";     }  else {         ($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);          }     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -