Floor the larger number
int, long and double are not precise enough. You should probably use BigInteger and/or BigDecimal instead.

Same problem with e=2.7182818284;. When you do 100000000000L*e you're already off by almost 6. And this is nothing compared to \$10^{100}\$.

Even if you use Math.E instead it's only 2.718281828459045. So you probably want to calculate e to an arbitrary precision (based on input n) instead. (Check here for a hint).



Note that none of those will help you with the time-limit-exceeded problem though. You'll have to find a smarter way so that you don't have to iterate all \$10^{4000}\$ numbers. Note that if you would add a number each millisecond and you ran the program for 300 years you would still only reach about \$10^{12}\$ or \$10^{13}\$. So no where close to what you want.

The best I can come up with is to keep track of the actual sum, the floored sum and the error on each order of magnitude.

So for n = 10, then n=100, then n=1000, ...

At each step you use the result of the previous one. That way you only need to do 10 iterations in the loop at that step to go up an order of magnitude.

Once you have those results you can calculate for any n the wanted result. The tricky part here is how to correct the error's at each step.

Even for \$n=10^{4000}\$ this should only result in O(10000) operations which (depending on your machine specs) runs in less than a minute. (Might also depend on how long calculations with BigInteger/BigDecimal take, I don't have much experience with those myself).

How to actually implement this I'll leave up to you, since this site is all about improving existing code, not just writing new code to solve the problem for you :)

Disclaimer: I did not test this myself yet, so not 100% sure that it will be fast enough even if implemented correctly.