package ajmas74.experiments; import java.math.BigDecimal; /** *
* This class generates as long a value of the Euler e as * possible, with BigInterger. This class has two methods * for caclulating e. The first method, called basicCalulateE (). * illustrates the basic algorithm with no optimisations, either * for precision or speed. The second method, called * caclulateE(), is written for precision and speed. *
** I make this class available so it could be a reference * to anyone interested. *
** For information on the algorithm, see the notes section * at the following link: * http://www.2dcurves.com/exponential/exponentiale.html *
** The 'largest' value of e I have managed to create with this class is: * 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509961818815930416903515988885193458072738667385894228792284998920868058257492796104841984443634632449684875602336248270419786232090021609902353043699418491463140934317381436405462531520961836908887070167683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350363 *
* @author Andre-John Mas - http://www.geocities.com/ajmas * */ public class CalculateE { static BigDecimal ZERO = new BigDecimal("0"); static BigDecimal ONE = new BigDecimal("1"); // --------------------------------------------------------------- // This section shows the basic implementation of a solution // for calculating e, along with a straight forward implementation // of factorial. // --------------------------------------------------------------- public static double factorial ( double n ) { if (n == 0) return 1; else return n * factorial(n-1); } public static double basicCalculateE ( double x ) { double n = 0; for ( int i=0; i<=x; i++ ) { n=n+(1/factorial(i)); } return n; } // --------------------------------------------------------------- // This next section caculates a large value of e, and also // is optimised to provide the best performance. Incremental // Factorial is implemented to improve factorial calculation // speed based on the current use. // --------------------------------------------------------------- /** * Version calculating e, using BigDecimal to allow for * a much larger number * * @param x the number of iterations * @return */ public static BigDecimal calculateE ( double x ) { IncrementalFactorial factoral = new IncrementalFactorial(); BigDecimal n = ZERO; for ( int i=0; i<=x; i++ ) { BigDecimal m = ONE.divide(factoral.next(),1000,BigDecimal.ROUND_HALF_EVEN); n = n.add(m); } return n; } /** This version of factorial is designed to reduce procesing * time. This is used on this basis that each call to the method * increases the number index by one. Therefore instead of * recalculating factorial every time, we simply multiply * the previous total with the current index. * 1=0, 1=1*1 * 2=1*2, 3=2*3 * etc. * */ static class IncrementalFactorial { static BigDecimal ONE = new BigDecimal("1"); int idx = 0; BigDecimal total = ONE; public BigDecimal next() { if (idx== 0) { total = ONE; } else { total = total.multiply(new BigDecimal(idx)); } idx++; return total; } } public static void main(String[] args) { int maxIterations = 9000; System.out.println("1) basicCalculateE, e = " + basicCalculateE(maxIterations)); System.out.println("2) CalculateE, e = " + calculateE(maxIterations)); } }