-3

I am look to code a system by where a given number is given by the user and then a script will work out the total number of combinations using that number all the way down to one.

so if the number was 10 it would need to find all the combinatins of ten numbers, nine numbers, 8 numbers and so on down to one!

So say if the number is 3

then you have

  • 3 number combinations (1,2,3) => 1
  • 2 number combinations (1,2)(1,3)(2,3) => 3
  • 1 number combinations (1)(2)(3) => 3

if the nuber is 4

then you would have

  • 4 number combinations (1,2,3,4) => 1
  • 3 number combinations (1,2,3)(1,2,4)(1,3,4)(2,3,4) => 4
  • 2 number combinations (1,2)(1,3)(1,4)(2,3)(2,4)(3,4) => 6
  • 1 number combinations (1)(2)(3)(4) => 4

and so on...

i wouldn't need different order combinations just the combinations itself and again the number could be anything though its unlikely to be over 10

does anyone know a way to code this so any given nubmer it will produce the combinations?

at the moment i have hard coded up to 5 numbers but its alot of coding, there has to be a simpler way of doing it :)

Hope this makes sense :) Any help/directions to go woud be hugely appreciated

Thanks

user2886669
  • 251
  • 1
  • 2
  • 12
  • If that is integer, then you just start from 1 and count to that number. Like if user gives "10", you just start from "1" and count to "10". If your "3" means "numbers with 3 digits", it just means that you need to count from "100" to "999", "4" will mean range "1000" to "9999". – Deele Jun 13 '14 at 12:41
  • how would that give you every combination? thanks – user2886669 Jun 13 '14 at 12:42
  • Explain, what do you mean saying "combination". – Deele Jun 13 '14 at 12:43
  • Do you want combinations or permutations? You're mixing them in your example above. – Jay Blanchard Jun 13 '14 at 12:44
  • Which scripting/programming language will you be using? – natronite Jun 13 '14 at 12:46
  • The OP tagged PHP @natronite – Jay Blanchard Jun 13 '14 at 12:47
  • http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers – Deele Jun 13 '14 at 12:48
  • i want every combination of numbers possible from a given number say 10 and i then want every combination of numbers from that number down to 1. sorry if im not being clear enough im trying to explain it clearly but its difficult to explain :( – user2886669 Jun 13 '14 at 12:48
  • like that deele but that count combinations where the number can appear in different slots ie 1,2,3,4 and 1,2,4,3 are counted as 2 combnations where as they both contain the same numbers i only want to find combinations which contain different number combinations like 1,2,3,4 and 1,2,3,5 for example – user2886669 Jun 13 '14 at 12:52
  • It's not difficult to explain @user2886669 it is just that you may not understand what you're asking for. As I pointed out before, you're mixing combinations and permutations in your result sets which makes it hard for us to determine what you want. In your 2 number combination of 3 numbers above you totally leave out the (2,3) combo. From what I am seeing it looks like you want a custom algorithm to generate certain number sets. – Jay Blanchard Jun 13 '14 at 12:53
  • @JayBlanchard sorry i missed out 2,3 that should be there – user2886669 Jun 13 '14 at 12:55

1 Answers1

1

If you just want to know how many combinations there are (regardless of the order of the elements within the combinations) use this formula:

n!/(k!(n-k)!) where n is how many numbers you have and k is how many numbers you have per combination. So if the number is 9 and you want to know how many combinations you can have grouping those numbers in pairs. it would give you: 9!/(2!(9-2)!) = 36

helpful: http://en.wikipedia.org/wiki/Combination

And as for coding it in php

$n = $_GET['number'];

echo "Combinations for $n numbers:";

for ($k = 1; $k <= $n; $k++) {
    $combinations = factorial($n)/(factorial($k)*factorial($n-$k));
    echo "<br>Grouped by $k:" . $combinations;
}


function factorial($number) {
    if ($number == 0) return 1;
    return $number * factorial($number - 1);
}

Edit: To print out all combinations:

combinations(range(1, $n), $k);

function combinations($numbers, $count, $prefix = ""){
    if ($count == 0) {
        echo "<br>". $prefix;
    } else {
        foreach ($numbers as $number) {
            $offset = array_search ( $number , $numbers)+1; 
            combinations(array_slice($numbers, $offset), $count-1, $prefix . $number);
        }
    }
}
natronite
  • 858
  • 8
  • 23
  • thanks for you answer but i need a way to return all the unique combinations of numbers for the given number not just the total combinations. plus will this count 1,2 and 2,1 as seperate combinations? thanks – user2886669 Jun 13 '14 at 12:58
  • Order of numbers is not important here, in @natronite's example. If you want all unique (order of numbers is important) combinations I will have to point out that your example for the number 3 has an error - there would be 6 possible combinations of 3 numbers instead of 1. (123)(231)(312)(321)(213)(132) – Jay Blanchard Jun 13 '14 at 13:01
  • yeah sorry by unique i mean the numbers in the combination not the order :) so (123) is the same as (231) as it contains the same numbers and there for would only require it once. thanks – user2886669 Jun 13 '14 at 13:17
  • just seen your edit natronite that is on the right lines i just need to to print out the actual combinations if at all possible? thanks – user2886669 Jun 13 '14 at 13:20
  • thanks for the updated code, i am getting an undefined function subset() error any ideas? thank you – user2886669 Jun 13 '14 at 16:56
  • My bad. I renamed the function and forgot one occurrence. Don't forget to accept the answer if it was helpful. – natronite Jun 13 '14 at 16:59
  • don't suppose it it possible to add some text between the numbers in each combination? i have found out how to add text before and after the entire combination but not after each number in the combination so if i could have something like (1 and 2 and 3 and 4 or) so "and" after each number except for the last number in the combination where it will be "or" is this possible? thanks – user2886669 Jun 14 '14 at 09:31