# what's the Mathematica command for a recursive formula?

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,

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

I would define

f = 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}

• Writing f=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). Apr 9, 2022 at 11:41
• 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}] Apr 9, 2022 at 12:38
• 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. Apr 13, 2022 at 21:11

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},
f[n], n]

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


Check:

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

(* Out= {1, 1, 1, 1/2, 7/48, 35/1152, 847/165888, 87241/119439360, \
62825983/687970713600, 2533206460543/249650812551168000, \
919252493608304383/905932868585678438400000} *)

• Thank you Dan for the nice command Apr 10, 2022 at 17:36
• This does not seem to be producing the correct answer. I suggest programming the explicit expression first, then working out more efficient alternatives. Apr 13, 2022 at 21:13
• It's the correct result @Kenric, just not the result you obtained. Not much else I can say. Apr 14, 2022 at 2:44
• For f you have an output of 1; however, f = f/0! + f/1! = 1 + 1 =2. So your solution does not give the correct sequence. Apr 14, 2022 at 19:46
• Using the notation in your comment, the problem is f[n] = Sum[f[j] / j!, {j, 0, n-1}]; f = 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. Apr 15, 2022 at 11:59

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 and f need to be set initially:

f = 1; f = 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 needs to be set explicitly:

f = 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.

• 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!. Apr 10, 2022 at 3:31
• Thank you for the efforts Apr 10, 2022 at 17:37
• @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. Apr 10, 2022 at 21:55
• 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. Apr 11, 2022 at 2:22
• @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. Apr 13, 2022 at 20:56

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

Just to illustrate Nest and NestList:

f := 1
f[n_ /; n >= 1] :=
Nest[Function[{x, y}, {x + x/Factorial[y], y + 1}] @@ # &, {1, 1},
n - 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}

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