Create variables in a for loop
Clear["Global`*"]
Continuing on with the suggestion by infinitezero to use recursion, use RSolve
to find the general expression.
ex[n_] = ex[n] /.
RSolve[{ex[0] == 1, ex[n] == ex[n - 1] + (1/2)^n/n!}, ex[n], n][[1]]
(* (Sqrt[E] Gamma[1 + n, 1/2])/Gamma[1 + n] *)
The first several values are
ex /@ Range[0, 10] *)
(* {1, 3/2, 13/8, 79/48, 211/128, 6331/3840, 75973/46080, 354541/215040, \
17017969/10321920, 306323443/185794560, 2042156287/1238630400}
Which are approximately,
% // N
(* {1., 1.5, 1.625, 1.64583, 1.64844, 1.6487, 1.64872, 1.64872, 1.64872, \
1.64872, 1.64872} *)
%[[-1]] // InputForm
(* 1.6487212706873657 *)
The limit of this sequence is
Limit[ex[n], n -> Infinity]
(* Sqrt[E] *)
% // N[%, 20] &
(* 1.6487212707001281468 *)
What about defining this recursively?
ex[0] = 1;
ex[n_] := ex[n] = ex[n - 1] + 0.5^n/n!;
The double assignment in the second line is very important. This causes all function calls to be evaluated only once. Once it has been initially evaluated, it will be saved in ex[n].
Table[{k, ex[k]}, {k, 1, 10}] // TableForm
1 1.5 2 1.625 3 1.64583 4 1.64844 5 1.6487 6 1.64872 7 1.64872 8 1.64872 9 1.64872 10 1.64872
One more way, using FoldList
:
FoldList[#1 + 0.5^#2/#2! &, 1 , Range[7]]
(* {1, 1.5, 1.625, 1.64583, 1.64844, 1.6487, 1.64872, 1.64872} *)