How to compute a table of numbers (all possibilities), where repetition is not allowed and order is not important. Example:

I have a set of prime numbers. In this example I have four: {3,5,7,11}, but it can be anything, and I want to choose every pair out of that set. To make things easier, I want to compute the indices to get those pairs of prime numbers. The set of indices is then {0,1,2,3}. We pick 2 out of 4 elements. So how do we compute the permutations or combinations:

0,1   (3,5)
0,2   (3,7)
0,3   (3,11)
1,2   (5,7)
1,3   (5,11)
2,3   (7,11)


It was difficult to find examples on the web, because they either allowed repetitions or were order was important. Pls answer with pseudocode or c/c++ if you can.

  • $\begingroup$ Are you specifically interested in pairs of elements? $\endgroup$
    – Steven
    Mar 24 at 14:00
  • $\begingroup$ What is meant by "a table of numbers (all possibilities)"? Can you state the task you are trying to solve more clearly? This is not a coding site; C/C++ code is off-topic here, but algorithms and methods are appropriate. $\endgroup$
    – D.W.
    Mar 24 at 17:53
  • $\begingroup$ @D.W. "Try to ask this question on cs.stackexchange.com – S.M. 21 hours ago" ref. stackoverflow.com/questions/66782338/… $\endgroup$ Mar 25 at 11:06

You seem interested in just pairs of indices. Then, if you have $n$ elements you can just generate all pairs of indices $(i,j)$ with $0 \le i < j < n$.

For i=0,1,...,n-2:
   For j=i+1, i+2, ..., n-1:
      Output (i,j)
  • $\begingroup$ That was exactly what I was looking for $\endgroup$ Mar 24 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.