Solution:
For all positive integers n, let ω(n)=f(n)−f(n−1). We claim that ω(n) is the largest odd divisor of n for all n>0. Indeed, for all positive integers k, we have
ω(2k)=f(2k)−f(2k−1)=f(k)+k2−(f(k−1)+k2)=f(k)−f(k−1)=ω(k)
and
ω(2k+1)=f(2k+1)−f(2k)=f(k)+(k+1)2−(f(k)+k2)=2k+1.
Inducting on the positive integers implies that ω(n) is indeed the largest odd divisor of n.
We can now rewrite the sum as
n=1∑31n(n+1)f(n)=n=1∑31(nf(n)−n+1f(n))=(n=1∑30nf(n)−f(n−1))−32f(31).
Note that by using the original recursive definition, we can compute
f(31)=162+82+42+22+12=341.
Moreover, we also see that nf(n)−f(n−1)=nω(n)=2−ν2(n), where 2ν2(n) is the largest power of 2 dividing n. Thus, our desired sum is
(n=1∑312−ν2(n))−32341=16⋅2−0+8⋅2−1+4⋅2−2+2⋅2−3+1⋅2−4−32341=32341.
Solution 2:
From the original recursion, for all positive integers n, we have
2n(2n+1)f(2n)=2n(2n+1)f(n)+n2=2n(2n+1)f(n)+2(2n+1)n
and
(2n+1)(2n+2)f(2n+1)=(2n+1)(2n+2)f(n)+(n+1)2=(2n+1)(2n+2)f(n)+2(2n+1)n+1.
Adding these two equations gives
2n(2n+1)f(2n)+(2n+1)(2n+2)f(2n+1)=2n(n+1)f(n)+21.
Thus, if T(n)=∑k=1nk(k+1)f(k), we have
T(2n+1)=k=1∑2n+1k(k+1)f(k)=1⋅2f(1)+m=1∑n(2m(2m+1)f(2m)+(2m+1)(2m+2)f(2m+1))
=21+m=1∑n(2m(m+1)f(m)+21)
=2n+1+21T(n).
Starting from T(1)=1, we can compute T(3)=45, T(7)=821, T(15)=1685, and finally, T(31)=32341.