# Can you transcribe this mathematica expression into a formula or pseudocode?

I want to understand the mathematca formula A282720

The description says

Number of nonzero terms in first n rows of the base-2 generalized Pascal triangle P_2


(But the Pascal Triangle has only nonzero terms, so the description makes nonsense).

His purpose is to generate the series:

0, 1, 3, 6, 9, 13, 18, 23, 27, 32, 39, 47, 54, 61, 69, 76, 81, 87, 96, 107, 117, 128, 141, 153, 162, 171, 183, 196, 207, 217, 228, 237, 243, 250, 261, 275, 288, 303, 321, 338, 351, 365, 384, 405, 423, 440, 459, 475, 486, 497, 513, 532, 549, 567


The Mathematica formula (on the same link) is:

Accumulate@ Prepend[Array[Sum[Mod[Binomial[# + k - 1, 2 k], 2], {k, 0, #}] &, 53], 0]


I'm not a Mathematica programmer, and don't understand what the formula means.

Can you translate it as an equation, or pseudocode?

I think that I understand the functions

Sum[Mod[Binomial


But I don't know what the # and & symbols means

Also Prepend[Array doesn't makes sense to me

Accumulate@ I guess, means that the result is series that is a progressive accumulation of terms

• The # and & symbols define an anonymous function where # is a slot for an argument. For example (# + 1 &) defines a function that adds one to its argument. # + 1 &[ 2 ] or shorthand # + 1 &[2] will both give 3. In your case the function is Sum[Mod[Binomial[# + k - 1, 2 k], 2], {k, 0, #}] & and it's applied by Array to numbers 1 through 53. Sep 7, 2020 at 16:01
• You could also do this in a more long-winded way. First define f[n_]:=Sum[Mod[Binomial[n + k - 1, 2 k], 2], {k, 0, n}], then you have Accumulate@ Prepend[Array[f, 53], 0] which returns the list: {0, f[1], f[1]+f[2], f[1]+f[2]+f[3], ..., (f[1]+f[2]+...+f[n])} Sep 7, 2020 at 16:03
• Array[f,n] will return {f[1] ,f[2], f[3], ..., f[n]} and the Prepend just sticks a zero at the front of the list. Sep 7, 2020 at 16:05
• Note that this sequence sums the binary digits of the numbers in Pascal's triangle. So, 2 contributes 1 to the total Sep 7, 2020 at 19:16

Sum[Mod[Binomial[# + k - 1, 2 k], 2], {k, 0, #}] &


& indicates that this is a function. # is the argument of the function. I transcribed it into mathematical notation for you, using $$n$$ instead of #.

$$f(n) = \sum_{k=0}^n \left[ \binom{n+k-1}{2k} \bmod 2 \right]$$

Array[..., 53]


This basically creates the sequence of numbers $$\bigl( f(1), f(2), \dots, f(53) \bigr)$$

Prepend[..., 0]


prepends a 0 to the sequence, so now we have $$\bigl(0, f(1), f(2), \dots, f(53) \bigr)$$

For the sake of simplicity, from here on I'll assume $$f(0) = 0$$, even though that is not consistent with the formula.

Accumulate computes partial sums, so the final result is $$g(n) = \sum_{i=0}^n f(i)$$

We can convert the above back to Mathematica code that follows the formulas more closely:

f[n_] := Sum[Mod[Binomial[n + k - 1, 2 k], 2], {k, 0, n}]
f[0]  := 0

Accumulate[ Table[f[n], {n, 0, 53}] ]