0
$\begingroup$

How to use a pure function to write following sum? I have tried to use FoldList, but it is still not working.

enter image description here

$\endgroup$
3
  • 2
    $\begingroup$ p = Total[(Rest@FoldList[Plus, 0, Range[#]])^-1] & $\endgroup$ – cvgmt May 4 at 15:12
  • $\begingroup$ it is not correct when i use n=10 $\endgroup$ – Udf Hx May 4 at 15:23
  • $\begingroup$ If you clear any old definitions (Clear["Global`*"]), solution provided by @cvgmt works as expected. $\endgroup$ – Bob Hanlon May 4 at 17:14
5
$\begingroup$

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*)    
$\endgroup$
5
$\begingroup$

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)*)
$\endgroup$
2
$\begingroup$
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]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.