# Is there a function for combinations?

**URL:** <https://fortran-lang.discourse.group/t/is-there-a-function-for-combinations/3501>\
**Category:** Help\
**Created:** [May 21, 2022, 8:07pm UTC](https://fortran-lang.discourse.group/t/is-there-a-function-for-combinations/3501 "2022-05-21T20:07:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorvz](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jorvz](https://fortran-lang.discourse.group/u/jorvz)\
**Post date:** [May 21, 2022, 8:07pm UTC](https://fortran-lang.discourse.group/t/is-there-a-function-for-combinations/3501/1 "2022-05-21T20:07:16Z")

</div>

Is there an internal function or library that takes an array and returns a matrix with the combination of the numbers in the array, for example the array (1,2,3,4) return ((1,2),(1,3),(1,4),(2,3),(2,4),(3,4)). I’m working on a collision simulation and need to keep track of the distance between the particles.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [May 21, 2022, 8:34pm UTC](https://fortran-lang.discourse.group/t/is-there-a-function-for-combinations/3501/2 "2022-05-21T20:34:43Z")

</div>

One of the codes of John Burkardt may have this:

**[TOMS515](https://people.math.sc.edu/Burkardt/f_src/toms515/toms515.html)** is a FORTRAN90 library which lists the subsets of size K selected from a set of size N, by Bill Buckles, Matthew Lybanon.

**[combo](https://people.sc.fsu.edu/~jburkardt/f_src/combo/combo.html)** , a FORTRAN90 code which includes routines for ranking, unranking, enumerating and randomly selecting balanced sequences, cycles, graphs, Gray codes, subsets, partitions, permutations, restricted growth functions, Pruefer codes and trees.

**[subset](https://people.sc.fsu.edu/~jburkardt/f_src/subset/subset.html)** , a FORTRAN90 code which enumerates, generates, randomizes, ranks and unranks combinatorial objects including combinations, compositions, Gray codes, index sets, partitions, permutations, polynomials, subsets, and Young tables. Backtracking routines are included to solve some combinatorial problems. Other routines handle continued fractions, Diophantine equations, and Pythagorean triples.

---

<div class="post-metadata">

**Author:** ![Abhilash](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/abhilash/32/1635_2.png) [@Abhilash](https://fortran-lang.discourse.group/u/Abhilash)\
**Post date:** [May 21, 2022, 11:13pm UTC](https://fortran-lang.discourse.group/t/is-there-a-function-for-combinations/3501/3 "2022-05-21T23:13:53Z")

</div>

If you require just pairs, it can be done as follows

```fortran
program main
   implicit none
   integer :: N,M, i,j, counter
   integer, allocatable:: array(:), nc2(:,:)

   array=[5,6,7,8,9]
   N=size(array)
   M=int(N*(N-1)/2)

   allocate(nC2(2,M))

   counter=1
   do i=1,N
      do j=i+1,N
         nc2(1,counter)=i
         nc2(2,counter)=j
         write(*,"(2(I0,x))")array(i),array(j)
         counter=counter+1
      enddo
   enddo

endprogram main

```
