javabigdecimaleulers-number

is there any way to get the sum of the product of euler's number and an integer, where the integer can range upto 10^6000?


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();
}

Solution

  • 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.