To be honest, this program doesn't really need much more optimization than limiting the number of terms to 2,

since the subsequent terms are much below the precision of

Double

.
For these two terms it is not a problem to convert the

Integer

s to

Double

s.

But assume these conversions are a problem.
We will show a way to avoid them.
The trick is to compute the terms incrementally.
We do not need to compute the factorials from scratch for each term,
instead we compute each term using the term before.