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


But I want the results to be as follows:


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:


And do


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

  • $\begingroup$ You can use Outer[] for this. $\endgroup$ Commented May 25, 2016 at 4:10
  • $\begingroup$ I figured it would have something to do with Outer, but I'm new enough I wasn't sure how to implement it. $\endgroup$ Commented May 25, 2016 at 4:11
  • $\begingroup$ 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. $\endgroup$ Commented May 25, 2016 at 4:14

2 Answers 2



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

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 *)
  • $\begingroup$ 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. $\endgroup$ Commented May 25, 2016 at 4:35
  • $\begingroup$ @Elem-Teach-w-Bach-n-Math-Ed. See the updated post. $\endgroup$
    – march
    Commented May 25, 2016 at 6:04

Got it myself:

list[x_, b_, c_] := Module[{list2},
  list2 = Table[{a, b, c}, {a, 0, c - 1}];
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[8], c], {c, 3 + 1, 5}]], {2}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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