2
$\begingroup$

I want to know the Mathematica command for

$$f(a)=\sum_{n=0}^{a-1} \frac{f(n)}{n!}, \quad f(0)=1$$

How to write $f(0)=1$ together with the summation? I used:

Sum[f[a_ ]=f[n]/n!, {n,0,a-1}] , f(0)=1

Sum[f[a_ ]=f[n]/n!, {n,0,a-1}] // f(0)=1

but both didn't work

I know we can avoid writing $f(0)=1$ by separating the first term as

$$f(a)=1+\sum_{n=1}^{a-1}\frac{f(n)}{n!}$$

but I need the command without separation.

Thanks,

$\endgroup$
4
  • $\begingroup$ f[0]=1 and f[a_]:=Sum[f[n]/n!,{n,0,a-1}] $\endgroup$
    – lxndr
    Apr 9, 2022 at 11:30
  • $\begingroup$ @lxndr can we write it in one line without using "and" ? $\endgroup$ Apr 9, 2022 at 11:33
  • 1
    $\begingroup$ You can write f[0]=1;f[a_]:=Sum[f[n]/n!,{n,0,a-1}] in one line, but that's the same as above in two lines. $\endgroup$
    – lxndr
    Apr 9, 2022 at 11:35
  • $\begingroup$ Thank you @lxndr it worked. All i wanted is that little sign ";" :) $\endgroup$ Apr 9, 2022 at 11:43

4 Answers 4

6
$\begingroup$

I would define

f[0] = 1;
f[n_ /; n >= 1] := f[n] = Sum[f[i]/i!, {i, 0, n - 1}]

This uses memoisation, which is not strictly necessary, but avoids unnecessary recomputation.

This gives

Table[f[n], {n, 0, 9}]
{1, 1, 2, 3, 7/2, 175/48, 4235/1152, 610687/165888,
439781881/119439360, 2533206460543/687970713600}
$\endgroup$
3
  • $\begingroup$ Writing f[0]=1 in front followed by ; then writing f[a_ ] := Sum[f[n]/n!, {n, 0, a - 1}] solved the issue. Thank you so much (+1). $\endgroup$ Apr 9, 2022 at 11:41
  • $\begingroup$ Since memoisation is most often used with integer values of the argument, you may want the second definition written as f[n_Integer?Positive] := f[n] = Sum[f[i]/n!, {i, 0, n - 1}] $\endgroup$
    – Bob Hanlon
    Apr 9, 2022 at 12:38
  • 1
    $\begingroup$ There is an error in this solution. The argument in the sum needs be f[i]/i! ; otherwise your always dividing by the same value n. I think the confusion arose because the original question used n as the incremental value; whereas n is more typically a constant. $\endgroup$
    – Kenric
    Apr 13, 2022 at 21:11
8
$\begingroup$

This can be solves as a recurrence. If you subtract an appropriate multiple of f[n-1] from f[n] then ther result of that is simply a multiple of f[n-1] (because all later terms were canceled).

recur = RSolveValue[{f[n] - (n - 1)!/n!*f[n - 1] == f[n - 1]/n!, f[1] == 1}, 
    f[n], n]

(* Out[70]= Product[(1 + (1 + K[1])! + K[1])/((1 + K[1])!*(1 + K[1])), {K[1], 
  1, -1 + n}] *)

Check:

In[72]:= Table[recur, {n, 0, 10}]

(* Out[72]= {1, 1, 1, 1/2, 7/48, 35/1152, 847/165888, 87241/119439360, \
62825983/687970713600, 2533206460543/249650812551168000, \
919252493608304383/905932868585678438400000} *)
$\endgroup$
10
  • $\begingroup$ Thank you Dan for the nice command $\endgroup$ Apr 10, 2022 at 17:36
  • $\begingroup$ This does not seem to be producing the correct answer. I suggest programming the explicit expression first, then working out more efficient alternatives. $\endgroup$
    – Kenric
    Apr 13, 2022 at 21:13
  • $\begingroup$ It's the correct result @Kenric, just not the result you obtained. Not much else I can say. $\endgroup$ Apr 14, 2022 at 2:44
  • $\begingroup$ For f[2] you have an output of 1; however, f[2] = f[0]/0! + f[1]/1! = 1 + 1 =2. So your solution does not give the correct sequence. $\endgroup$
    – Kenric
    Apr 14, 2022 at 19:46
  • 1
    $\begingroup$ Using the notation in your comment, the problem is f[n] = Sum[f[j] / j!, {j, 0, n-1}]; f[0] = 1; The confusion in the original question is in using "a" as the argument and "n" as the increment of the sum, which is not typical. $\endgroup$
    – Kenric
    Apr 15, 2022 at 11:59
4
$\begingroup$

Recursion involves setting explicitly the initial solution(s) and then defining a function that calls the previous answer and combines this with the incremental change. I'm going to use different variables. n is typically a constant, so I'll define f[n]; and i is a typical increment for sums. In your problem both f[0] and f[1] need to be set initially:

f[0] = 1; f[1] = 1;
f[n_Integer/;n >= 2] := f[n] = f[n-1] + f[n-1]/(n-1)!

Here are the first few instantiations:

f[#] & /@ {0, 1, 2, 3, 4, 5, 6}
{1, 1, 2, 3, 7/2, 175/48, 4235/1152}

And this expression does not try to compute NonNegative Integer inputs:

f[#] & /@ {-1, 1.2, -2.3, 2/4}
{f[-1], f[1.2], f[-2.3], f[1/2]}

For comparison, the explicit expression can also be coded in Mathematica. Here just f[0] needs to be set explicitly:

f[0] = 1;
f[n_Integer?Positive] := f[n] = Sum[f[i]/i!,{i,0,n-1}];

and the output is identical:

f[#] & /@ {0, 1, 2, 3, 4, 5, 6} 
{1, 1, 2, 3, 7/2, 175/48, 4235/1152}

While both solutions work, the recursive solution will be efficient for large values of n, since it does not have to recompute the previously solved lower solutions. Although the summation also saves previous values, the full summation is computed for each new value of n.

$\endgroup$
11
  • 1
    $\begingroup$ This is a good method but I think the coefficients are not quite right because the recurrence for f[n-1] has denominator (n-1)!` rather than n!. $\endgroup$ Apr 10, 2022 at 3:31
  • $\begingroup$ Thank you for the efforts $\endgroup$ Apr 10, 2022 at 17:37
  • $\begingroup$ @DanielLichtblau I believe the expression is correct. In the question the sum is from 0 to a-1, so last summation is f[a-1]/f[a-1]. I've used n instead of a. Also, it's not possible for the expression on the right to include f[n] since this is what's be computed. $\endgroup$
    – Kenric
    Apr 10, 2022 at 21:55
  • $\begingroup$ I made no claim that f[n] should be on the right (though as an equation there is a symmetry property so one could put it on either side). The issue is that f[n-1] is defined using a denominator of (n-1)! whereas the denominator in defining f[n] is instead n!. If you compare your results to othe responses you will find that they differ. This denominator issue is the cause of the differences. $\endgroup$ Apr 11, 2022 at 2:22
  • $\begingroup$ @DanielLichtblau, Thank you. The function is correct; however, there was something incorrect about my stated answer. To clarify this, I've rewritten the function in terms of f[a] like the original and added the exact expression as stated for comparision. $\endgroup$
    – Kenric
    Apr 13, 2022 at 20:56
0
$\begingroup$

This is corrected version (I have deleted previous incorrect answer)

Just to illustrate Nest and NestList:

f[0] := 1
f[n_ /; n >= 1] := 
 Nest[Function[{x, y}, {x + x/Factorial[y], y + 1}] @@ # &, {1, 1}, 
   n - 1][[1]]

So, f/@Range[0,10] yields:

{1, 1, 2, 3, 7/2, 175/48, 4235/1152, 610687/165888,
439781881/119439360, 2533206460543/687970713600,
919252493608304383/249650812551168000}

$\endgroup$
2
  • $\begingroup$ Mikado and Kenric's solutions now show the correct sequence. You may want to check your approach against these solutions. $\endgroup$
    – Kenric
    Apr 14, 2022 at 19:55
  • $\begingroup$ @Kenric thank you. I will correct. I now see my error $\endgroup$
    – ubpdqn
    Apr 14, 2022 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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