How to generate all n-choose-k combinations?

19 views
Skip to first unread message

Andrew Gill

unread,
Sep 2, 2025, 5:16:01 AMSep 2
to MiniZinc
I want an array[1..n_choose_k,1..k] of int: combos but don't know how to (a) calculate n_choose_k=n!/((k!(n-k)!) and (b) enumerate all of the combos of k integers from the first n positive integers :-(. 

I looked through the MiniZinc Handbook but couldn't find either of these, but surely this has been done before?!

Any pointers gratefully accepted.

Andrew

Boris Okner

unread,
Sep 2, 2025, 7:11:14 PMSep 2
to mini...@googlegroups.com
If this is for static data, I would generate .dzn file using your favorite host language, even if it was possible to generate inline data with MiniZinc.

--
You received this message because you are subscribed to the Google Groups "MiniZinc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minizinc+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/minizinc/c75f93ab-68e5-44d0-a1aa-734bb3da9e8en%40googlegroups.com.

Andrew Gill

unread,
Sep 2, 2025, 8:31:42 PMSep 2
to MiniZinc
Thanks Boris. I am getting the sense that 'hard-coding' data in MiniZinc is preferred (presumably for compile/compute times?), although its not as pretty!

d...@irvine.com

unread,
Sep 2, 2025, 9:13:07 PMSep 2
to mini...@googlegroups.com, Andrew Gill
Hi Andrew,
For factorial, see
https://gist.github.com/jarble/f8a9d0f99c106837401bbb9b22b1ecb2
which has:

function int:factorial(int:t) =
if t = 0 then 1 else t * factorial(t-1) endif;

For enumeration, I think you want alldifferent and strictly_increasing,
both of which are global predicates.

-- Dan
>>> [1].
>
> --
> You received this message because you are subscribed to the Google
> Groups "MiniZinc" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to minizinc+u...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/minizinc/a32eb879-8143-4eef-9942-c7883d15bc62n%40googlegroups.com
> [2].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/minizinc/c75f93ab-68e5-44d0-a1aa-734bb3da9e8en%40googlegroups.com?utm_medium=email&utm_source=footer
> [2]
> https://groups.google.com/d/msgid/minizinc/a32eb879-8143-4eef-9942-c7883d15bc62n%40googlegroups.com?utm_medium=email&utm_source=footer
Reply all
Reply to author
Forward
0 new messages