I noticed that Reap
unpacks packed arrays. I've looked at previous answers but am still an unsure whether this a serious issue.
E.g. consider Ex 47.3 from EIWL:
Use Sow
and Reap
to get a list of all cases where #/2
is used in Nest[If[EvenQ[#], #/2, 3#+1]&, 1000, 20]
.
One solution is
On["Packing"]
Reap @ Nest[If[EvenQ[#], Sow[#]/2, 3#+1]&, 1000, 20]
but one gets multiple (FromPackedArray
) messages about unpacking arrays due, I assume, to the fact that intermediate results are irregular structures.
Since other users don't appear to see this, here's a screenshot:
BTW, since $3x+1$ is always even, a more efficient coding of the Collatz problem is to replace 3#+1
by (3#+1)/2
.
coll = Reap[Nest[If[EvenQ[#], Sow[#]/2, 3 # + 1] &, 1000, 20]]; coll[[-1, 1]] // Developer`PackedArrayQ
returnsTrue
. $\endgroup$TraditionalForm
as your default output format, and creating those boxes is generating the unpacking messages. For example, adding a;
at the end of theReap
will prevent the messages (until you display the output). $\endgroup$