#### The First n Digits of e

```/* e(N, E) is true if the list E contains the first N digits of e.           */
/* e.g. e(30, [2,7,1,8,2,8,1,8,2,8,4,5,9,0,4,5,2,3,5,3,6,0,2,8,7,4,7,1,3,5]).*/
e(N, E):-
N > 0,
Length is (N * 10) // 4,
copies(Length, 1, As),
e_1(N, Length, 0, 2, As, [], E0),
reverse(E0, E).

e_1(0, _, _, _, _, E, E):-!.
e_1(J, Length, Nines0, Previous0, As, E0, E):-
next_x(Length, 0, As, Bs, X),
next_digits(X, J, J1, Nines0, Nines, Previous0, Previous, E0, E1),
e_1(J1, Length, Nines, Previous, Bs, E1, E).

next_x(0, Q, [], [], Q):-!.
next_x(I, Q, [A|As], [B|Bs], X):-
T is 10 * A + Q,
B is T mod (I + 1),
Q1 is T // (I + 1),
I1 is I - 1,
next_x(I1, Q1, As, Bs, X).

next_digits(X, J, J, Nines0, Nines, Previous, Previous, E, E):-
X mod 10 =:= 9, !,
Nines is Nines0 + 1.
next_digits(X, J0, J, Nines0, Nines, Previous0, Previous, E0, E):-
J1 is J0 - 1,
Digit is Previous0 + X // 10,
Digit1 is 9 * (1 - X // 10),
append_digits(J1, J, Nines0, Nines, Digit1, [Digit|E0], E),
Previous is X mod 10.

append_digits(J0, J, N0, N, Digit, E0, E):-
J0 > 0,
N0 > 0, !,
J1 is J0 - 1,
N1 is N0 - 1,
append_digits(J1, J, N1, N, Digit, [Digit|E0], E).
append_digits(J, J, N, N, _, E, E).

/* copies(N, X, Xs) is true if the list Xs contains N occurrences of the     */
/*   item X.                                                                 */
copies(0, _, []):-!.
copies(I, X, [X|Xs]):-
I > 0,
I1 is I - 1,
copies(I1, X, Xs).

/* reverse(Xs, Ys) is true if Ys is the result of reversing the order of     */
/*   the elements in the list Xs.                                            */
%reverse(Xs, Ys):-reverse_1(Xs, [], Ys).

%reverse_1([], As, As).
%reverse_1([X|Xs], As, Ys):-reverse_1(Xs, [X|As], Ys).
```