Question about a "combinations" algorithm - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Question about a "combinations" algorithm (/thread-231505.html) |
Question about a "combinations" algorithm - Namir - 09-19-2012 Hell All, Anyone knows a good link to some (C++, Pascal, BASIC, or Visual Basic) source code to generate different combinations of an array of elements. I am working with a problem where I generate an array of six elements (each being a random value between 1 and 10). I want to be able to generate all possible combinations of these six elements. Thanks!
Namir Edited: 19 Sept 2012, 4:32 p.m.
Re: Question about a "combinations" algorithm - Kiyoshi Akima - 09-19-2012 Brute-force C/C++: { Re: Question about a "combinations" algorithm - Kiyoshi Akima - 09-19-2012 Or did I misunderstand the problem? My code fragment generates all possible combinations of six numbers each in the range 1 to 10.
Are you generating an array of six numbers and then want all the various combinations of those six numbers? All the one-number combinations, all the two-number combinations, and so forth?
Re: Question about a "combinations" algorithm - Marcel Samek - 09-19-2012 Just to make the question perfectly clear:
1) Are you talking about combinations or permutations
M.
Re: Question about a "combinations" algorithm - Paul Dale - 09-19-2012 C++ is easy. The standard template library include this algorithm.
#include <algorithm> Then call:
next_permutation(array, array+n)
Re: Question about a "combinations" algorithm - Namir - 09-19-2012 Yes, I have generated the arrays of six numbers separately. Now I want to create all possible combinations of values based on the values in this array.
Re: Question about a "combinations" algorithm - Namir - 09-19-2012 Yes you can repeat the elements,
Re: Question about a "combinations" algorithm - Marcus von Cube, Germany - 09-20-2012 If you allow repetitions I'd just create all 6 digit base 6 numbers and use their digits as indexes into the array of numbers.
Re: Question about a "combinations" algorithm - David Hayden - 09-20-2012 Here it is in C++. I think this is the same algorithm as the one Pauli mentioned. The main entry point is permute(). This version calls processResult() on each permutation.
// Process the results of permuting. This gets called once per permutation. Re: Question about a "combinations" algorithm - Gilles Carpentier - 09-20-2012 Hi Namir, if I understand well your problem, here is a 39gII program :
EXPORT Perm(k,s) Usage : Perm(5,{"a","b","c","d"}) Return the 5th permutation of the set. -> {"d","b","c",a"} As there are SIZE(s)! possible permutations,this program displays all the possible permutations
With 6 elements in the list, there is 720 permutations PS for TIM : A SWAP command would be welcome on the 39gII ;)
a:=(k MOD j)+1; b:=s(a); s(a):=s(j); s(j):=b; PS : I realise that perhaps you also want solutions like :
{ "a" } {"a" ,"b" } with {"a","b","c","d"} input ?? Edited: 20 Sept 2012, 6:02 p.m.
|