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).
Subsets[Range@1000, All, {1*^100, 1*^100 + 10}]
, works fine. $\endgroup$