How to use a pure function to write following sum?
I have tried to use FoldList
, but it is still not working.
3 Answers
Try RSolve
\[Rho] = RSolveValue[{rho[n] - rho[n - 1] == 1/Sum[i, {i, 1, n}], rho[1] == 1}, rho, n]
(*Function[{n}, (2 n)/(1 + n)]*)
\[Rho][10]
(*20/11*)
One does not need to invent something complicated, just write what you see
f[n_] := Sum[1/Sum[k, {k, m}], {m, n}]
f[10]
(*20/11*)
and in pure form
g = Sum[1/Sum[k, {k, m}], {m, #}] &
g@10
g@t
(*20/11*)
(*2 - 2/(1 + t)*)
p = Function[n, Sum[2/k/(k + 1), {k, 1, n}]];
p[10] (* 20/11 *)
If you are really committed to using Fold
, you could try:
p = Function[n, Last@Fold[
With[{dn = #1[[1]] + #2}, {dn, #1[[2]] + 1/dn}] &,
{0, 0}, GeneralUtilities`RangeIterator[n]]]
p = Total[(Rest@FoldList[Plus, 0, Range[#]])^-1] &
$\endgroup$Clear["Global`*"]
), solution provided by @cvgmt works as expected. $\endgroup$