# Uniting lists of lists

Here's what I'm trying to do. First a simple example:

list[a_, b_, c_] := Module[{d},
d = Union[
Range[a, Prime[b], Prime[c]],
Range[a + 2, Prime[b], Prime[c]]]]

list[1, 8, 2]
{1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19}


Then I'd like to do

list2=Union[Table[list[Table[a,{a,0,c}],8,c,]],{c,3,5}]]


But I want the results to be as follows:

Union[list[0,8,3],list[0,8,4],list[0,8,5]],
Union[list[0,8,3],list[0,8,4],list[1,8,5]],
Union[list[0,8,3],list[0,8,4],list[2,8,5]],
Union[list[0,8,3],list[0,8,4],list[3,8,5]],
Union[list[0,8,3],list[0,8,4],list[4,8,5]],
Union[list[0,8,3],list[1,8,4],list[0,8,5]],
Union[list[0,8,3],list[1,8,4],list[1,8,5]],
...
Union[list[0,8,3],list[3,8,4],list[4,8,5]]
Union[list[1,8,3],list[0,8,4],list[0,8,5]]
...
Union[list[2,8,3],list[3,8,4],list[4,8,5]]


So, at present, my "list2" code isn't right, and I'm not sure what to do. After I get this fixed, then I'd like to create a list of primes:

plist=Table[Prime[p],{p,1,8}]


And do

Max[Max[Differences[Complement[plist,list2]]]]


so that 60 lists are created, each being the list of primes with a different line of list2, as outlined above, subtracted out. Then, each of those lists becomes a list of differences. Then the Max of those differences is found for each list. Then the max of all those max differences is found.

How can I get from where I am to where I want? Thanks

• You can use Outer[] for this. – J. M.'s discontentment May 25 '16 at 4:10
• I figured it would have something to do with Outer, but I'm new enough I wasn't sure how to implement it. – Elem-Teach-w-Bach-n-Math-Ed May 25 '16 at 4:11
• A guiding tip: the last argument will be critical, since you need to tell Outer[] that you will be taking a generalized outer product of a list of lists. This last argument will tell Outer[] to account for that. – J. M.'s discontentment May 25 '16 at 4:14

Defining

Clear@list
list[a_, b_, c_] := Union[Range[a, Prime[b], Prime[c]], Range[a + 2, Prime[b], Prime[c]]]


we can get your list of Unions using the function

Clear@list2
list2[n_, range_List] :=  Union @@ MapThread[list[#1, n, #2] &, {#, range}] & /@ Tuples[Range[0, # - 1] & /@ range];


where n is the 8 and range is the possible values for c. So, the usage could be

list2[8, Range[3, 5]]


to get the list in the OP above.

Then, using

plist = Table[Prime[p], {p, 1, 8}];


we do

Differences@Complement[plist, #] & /@ list2[8, Range[3, 5]] // Flatten // Max
(* 16 *)

• This got it for my specific example, but isn't able to be manipulated in the way I want. Say for instance instead of {$c,3,5$} I wanted to do {$c,8,15$}? I'd have to completely rewrite the code each time rather than just changing the c range. – Elem-Teach-w-Bach-n-Math-Ed May 25 '16 at 4:35
• @Elem-Teach-w-Bach-n-Math-Ed. See the updated post. – march May 25 '16 at 6:04

Got it myself:

list[x_, b_, c_] := Module[{list2},
list2 = Table[{a, b, c}, {a, 0, c - 1}];
list2]
list3[a_, b_, c_] := Union[
Range[a, b, Prime[c]],
Range[a + 2, b, Prime[c]]]
list4 = Union @@@ Apply[list3, Tuples[Table[list[3, Prime, c], {c, 3 + 1, 5}]], {2}]