in reply to Challenge: Number of unique ways to reach target sum

This can speed the calculation to twice three times faster with a minimal change in the program, and eight four times faster if you

Here's the resulting faster variant of my script: (Update: I've removed the redundant code from this snippet, as noted on Re: Challenge: Number of unique ways to reach target sum and updated the benchmarks accordingly)

This of course works only in those solutions which calculate the number of possibilities only, not for those that iterate on them all.

Update: blokhead's reply has another interesting optimization.

It uses the fact that if if t is too small or too large, there'll be no solutions. (Too small means t < c * (c + 1) / 2.) I'm not doing that, but at least I check for t being too small when I'm calculating a value. I don't have to check for it being too large explicitly, as that's taken care by the symmetry. This optimization gives a 20% 15% speed improvement. Some other optimizations (not that the big ifelse statement has disappeared completely) give some extra improvement,

Update:

like blokhead does makes it even more faster (with 40%). Here's the newer code.

Update: Improving this a tad bit more, we get