How to use AppendTo in do loop?

list = {1, E, (3/2)*E^2 + (3/8), (5/2) E^3 + (25/8)};
AppendTo[list, 2*E*list[[{x - 1}]] + (2*x - 3) (2*x - 4) (2*x - 5) (1/4)*list[[{x - 2}]]]

Let x from 5 to 10, add terms to the list. I have already try

list = {1, E, (3/2)*E^2 + (3/8), (5/2) E^3 + (25/8)};x = 5 ;AppendTo[list,2*E*list[[{x - 1}]] + (2*x - 3) (2*x - 4) (2*x - 5) (1/4)*list[[{x - 2}]]]

The result is correct. But

Do[AppendTo[list, 2*E*list[[{x - 1}]] + (2*x - 3) (2*x - 4) (2*x - 5) (1/4)*list[[{x - 2}]]],{x,5,10}]

The result is NOT the same as the expectation.

How to edit to solve it?

• Why do you use list[[{x - 2}]] instead of list[[x - 2]] ? Oct 16 '21 at 11:50
• Generally, don't use AppendTo in a loop. Learn to use Reap and Sow. Or where possible use Nest, Table, Map and things like that instead. Oct 16 '21 at 11:57
• This looks like a job for RecurrenceTable. Oct 16 '21 at 12:03

Using Nest:

In[]:= res = Nest[
Apply @ Function[{x, list},
{
x + 1,
Append[
list,
FullSimplify[
2*E*list[[x - 1]] + (2*x - 3) (2*x - 4) (2*x - 5) (1/4)*list[[x - 2]]]
]
}
]
,
(* {x, initial list} *)
{4, {1, E, (3/2)*E^2 + (3/8), (5/2) E^3 + (25/8)}}
,
6 (* to go through 5 to 10 *)
]

Out[]= {
10,
{
1,
E,
3/8 + (3 E^2)/2, 25/8 + (5 E^3)/2,
(63 E)/4 + 3 E^3,
5/16 (63 + 4 E (5 + 63 E + 4 E^3)),
3/4 (525 + 42 E^2 + 420 E^3 + 8 E^4),
5/2 E (1575 + E (5 + 4 E (90 + E^2))),
3/16 (45045 + 4 E (4625 + E (45045 + 4 E (21 + E (925 + 4 E))))),
5/8 (429975 + 2 E^2 (23499 + 2 E (86005 + 2358 E + 8 E^3)))
}
}

And to get the result (without the step), just res[[-1]].