-2

So I tried using BigDecimal which is giving me the value of Euler's number up to 50 decimal points. But since the constraint value is this high the number can range from 1 to 10^6000, and hence I am not getting any precision(making it optimal is another issue). Here is the block of code:

for(int i=1; i<n+1; i++){
 // i is the integer value and bd is the value of the euler's number, so below i am multiplying the value of i with the euler's number
 BigDecimal x = BigDecimal.valueOf(i).multiply(bd);
 //Here i am taking floor of the above calculated value
 x.setScale(0, RoundingMode.HALF_UP);
 //and here sum is floor[1*euler's no] + floor[2*euler's number] + ... + floor[n*euler's number] 
 sum += x.intValue();
}
Orion
  • 11
  • 2
  • 1
    What is `sum`? You might need to make that a BigDecimal as well. – lucasvw Jun 08 '17 at 12:20
  • 2
    The code snippet suggests you want to sum up 10^6000 numbers: good luck with that. – Henry Jun 08 '17 at 12:20
  • I first wanted to suggest using the formula `(n*(n+1) / 2)` and multiplying it with bd *once*, and then truncating, but that did not take into account that you need to add the the floor values, so 1*e would become 2, 2*e would become 5, giving a subtotal of 7, etc.while 3*e would become 8, giving an error of 1 already. You will have to loop, I'm afraid. – Rudy Velthuis Jun 26 '17 at 16:08

1 Answers1

0

I think you are asking how to compute Euler's number (i.e. e) to greater precision.

The answer would be to implement an algorithm that converges rapidly. With a little bit of research, I found this page on "Calculating the Value of e". At the bottom, it describes Brothers Formula:

e =​ Sum(n=0, n=​∞​​​, (2n+2)​​ / (2n+1)!​​ )

which converges very rapidly. You could code that in Java using BigDecimal to calculate to any number of digits precision that you need.

See also:

which (if I understood it correctly!) gives a more complicated formula that converges even more rapidly.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • You understood correctly. The link to the pdf is very valuable, thanks. I've been looking for something like it, and there is not much (or at least not much I could read and follow) on that subject.. – Rudy Velthuis Jun 26 '17 at 15:59