Oftentimes I am searching for the first item in a sequence that satisfies some property. For example, consider the subsets of prime numbers whose total is prime. To start generate them, we could naively try Subsets[Range[100]], which fails (as it should) yielding the message:

Subsets::toomany: The number of subsets (1267650600228229401496703205376) is too 
large; it must be a machine integer. >>

But is it too much to ask for a list of the first 500 subsets (by any canonical ordering), or perhaps just the nth subset?

Ok, so I found the following in the details section, which gives (if possible) the sth subset:

Subsets[list, nspec, {s}]

But sadly, this is not at all what I need - according to the docs:

Subsets[list, nspec, spec] gives the same result as Take[Subsets[list, nspec], spec], provided that the elements specified by spec are present.

So Mathematica performs the entire computation and only when finished (and it won't finish) does it perform a Take at the end. I would appreciate some guidance as to the preferred tactics when dealing with this problem of , i.e. "how to tell Mathematica start processing the intermediate results on the fly" (like you can with AsynchronousTaskObject and friends).

  • 1
    $\begingroup$ See (9554) $\endgroup$ Jul 31, 2015 at 2:45
  • 2
    $\begingroup$ What are you talking about? The docs make it clear: it gets the same results, NOT that it does the same thing (do the work, then select). Try eg Subsets[Range@1000, All, {1*^100, 1*^100 + 10}], works fine. $\endgroup$
    – ciao
    Jul 31, 2015 at 3:08
  • $\begingroup$ The 1st item in a list of all subsets of the prime numbers in canonical order for which the elements total to a prime is the set {2}. I make this comment to suggest you might want to place constraints of the size of the subsets. $\endgroup$
    – m_goldberg
    Jul 31, 2015 at 4:09
  • $\begingroup$ Also related: (75431) $\endgroup$
    – Mr.Wizard
    Jul 31, 2015 at 4:49
  • 1
    $\begingroup$ @Mr.Wizard: Whoa - is that a bug in SE? I marked the question for close based on "...easily found in the documentaton...", not as duplicate (though I'd agree). Or did I mark it as the latter, and need to cut back on the opium consumption? $\endgroup$
    – ciao
    Jul 31, 2015 at 5:03


Browse other questions tagged or ask your own question.