# All Possibilities to skip up to n numbers between a range of numbers

I want to get every possibility to skip up to n numbers between a range of numbers from 0 to m+n-1, whereby m is a self-chosen number. The length of the list should stay constant and should always start with 0. Furthermore, every possibility should get a name. I have already made it for n=1 and m=4:

m = 4; For[i = 1, i < m, i++, Subscript[a, i] = Range[0, m] /. {i -> Sequence[]}; Print[Subscript[a, i]]]

which gives me: {0,2,3,4} {0,1,3,4} {0,1,2,4}

For n=2 there would be 6 possibilities: {0,3,4,5} {0,1,4,5} {0,1,2,5} {0,2,4,5} {0,2,3,5} {0,1,3,5}

Any ideas?

• Like With[{m = 4, n = 2}, Prepend[Complement[Range[m + n - 1], #], 0] & /@ Subsets[Range[m + n - 1], {n}]]? Commented Apr 1, 2017 at 19:43
• With[{m = 4, n = 2}, Complement[Range[0, m + n - 1], #] & /@ Subsets[Range[m + n - 2], {n}]] wliminates lists that do not end in 5 and also is a little shorter. Commented Apr 1, 2017 at 19:50
• Nearly. Every list should end with the number m+n-1 (like you see in the OP) and should get a name. Commented Apr 1, 2017 at 19:53
• Oh great! What would be the shortest way to give every list an own name? Subscript[a, 1] being the first list, Subscript[a, 2] the second and so on... Commented Apr 1, 2017 at 19:59

The following appears to meet your need:

With[{m = 4, n = 2}, Complement[Range[0, m + n - 1], #] & /@ Subsets[Range[m + n - 2],
{n}]];
Table[Subscript[a, i] = %[[i]], {i, Length[%]}]
{{0, 3, 4, 5}, {0, 2, 4, 5}, {0, 2, 3, 5}, {0, 1, 4, 5}, {0, 1, 3, 5}, {0, 1, 2, 5}}


Then, for instance,

Subscript[a, 4]
(* {0, 1, 4, 5} *)

• Lets assume n stays constant and m is defined through the length of a matrix. I have some matrixes and they are in a list: j = {a,b,c};, whereby: a = {{1}, {2}, {3}, {4}}; b = {{1}, {2}, {3}, {4}, {5}}; c = {{1}, {2}, {3}, {4}, {5}, {6}}; Now I want to get all possible outcomes for these different m's. I tried the following: For[k = 1, k <= Length[j], k++, With[{m = Length[j[[k]]], n = 1}, Complement[Range[0, m + n - 1], #] & /@ Subsets[Range[m + n - 2], {n}]]; Print[Table[Subscript[a, i] = %[[i]], {i, Length[%]}]] ] But it gives me 3 times the same answer. Commented Apr 2, 2017 at 23:33
• Using % to refer to the previous output does not work inside For, etc. Instead, use For[k = 1, k <= Length[j], k++, With[{m = Length[j[[k]]], n = 1}, tem = Complement[Range[0, m + n - 1], #] & /@ Subsets[Range[m + n - 2], {n}]]; Print[Table[Subscript[a, i] = tem[[i]], {i, Length[tem]}]]] Commented Apr 3, 2017 at 15:05
• I see.. thank you! Commented Apr 3, 2017 at 15:56

Any technique using generation of all possible outcomes will be limited to quite small inputs. If you have a need for larger cases, this might be useful:

fn = First[ArrayFlatten[{{0, Subsets[Range[#1 + #2 - 2],
{#1-2}, {Binomial[#1 + #2 - 2, #1 - 2] - #3 + 1}], #1 + #2 - 1}}]] &;


Arguments are M, N, index (e.g. your "Name").

For example, for m, n of 500, 200, the millionth result (in your order) is produced with:

fn[500, 200, 1000000] // Short


{0,198,199,200,201,202,203,204,205,<<482>>,691,692,693,694,695,696,697,698,699}

Generation directly of all outcomes of such size is o/c preposterous: there are on the order of 10^180 lists:

fn[500,200,1283451590884490155627322513636141080670987268936415463872897835448843713416725942595587643683967533654739594029917521720680418278735279486033451445209995803913285527115719519688200] // Short
`

{0,1,2,3,4,5,6,7,8,9,10,11,<<476>>,488,489,490,491,492,493,494,495,496,497,498,699}