Previous page : Numbers, an introduction
Next page : Finding 10000 decimals of pi
In the following pages 10 000 decimals of e are calculated using the standard series expansion formula. The first set of calculations are done using JavaScripts native bigInt value type, but interpreted as fixed int numbers, where 1 is replaced by a value 10 to the power of some number. In this way all the calculations can be done using integers only. When printing out the result the decimal point is added.
The actual calculation is not more than this:
- A slow variant showing every step (about one every frame on your computer, usually 60 per second)
- A faster variant, calculating the result in one go
The above are (2026-04-06) quite mutch faster using Chrome than Firefox. We hare talking about a factor of three faster.
The second set is calulated using ordinary floating point numbers, but they are threated as 5 digit integers. That means that we have to write our own addition and division routines. This is quite much faster than using the bigint routines, but the code is on the other hand about 20% bigger.
- A slow variant showing every step (about one every frame on your computer, usually 60 per second)
- A faster variant, calculating the result in one go
Below are the code for the fast variant of the above.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
"use strict"; /************************************************* * Fast E 1.0 by Mauritz Blomqvist 2026-04-06 * This uses ordinary numbers to calculate e, this is * much faster than using big integers. ************************************************/ let n = 2005; let digitsPerNumber = 5; let base = 10 ** digitsPerNumber; let e = Array(n).fill(0); e[0] = 2; let num1 = Array(n).fill(0); let num2 = Array(n).fill(0); num1[1] = base; let div = 1; let cnt = 1; let divisor = 1; let expText = document.getElementById("expText"); let addText = document.getElementById("addText"); function print(paragraph, numberArray) { let str = "2."; for (let i = 1; i < numberArray.length - 4; i++) { let part = numberArray[i].toString().padStart(digitsPerNumber, "0"); str += part + " "; if (i % 16 === 0) { str += "<br>"; if (i > 1) str += "&nbsp; &nbsp;"; } } paragraph.innerHTML = str; } // Division of a big integer by a small integer, // the result is stored in result. // If the dividend at the current position is 0, // the result is set to 0 and a the start position // counter is increased by one, the next position is used. // This saves time by skipping leading zeros in the dividend, // which is important for faster computation of the later terms // in the series. function divide(dividend, divisor, result) { let res = 0; if (dividend[cnt] === 0) { result[cnt] = 0; cnt++; } for (let i = cnt; i < n; i++) { res = (res - result[i - 1] * divisor) * base + dividend[i]; result[i] = Math.floor(res / divisor); } } // Addition of two big integers, the result is stored in term2. // If the sum at the current position exceeds the base, the excess is // subtracted from the current position and added to the next position, // this is done for all positions. function add(term1, term2) { for (let i = n - 1; i > 0; i--) { term2[i] += term1[i]; if (term2[i] >= base) { term2[i] -= base; term2[i - 1]++; } } } // The main loop of the program, it alternately divides num1 and num2 // by the current divisor, and the result is stored in the other array, // then the result is added to e. // The digits of e are printed after // each term is calculated. // The loop continues until the desired number of terms have been calculated, // which is determined by when the first non-zero digit is beyond n. // The cnt variable is updated in the divide function. let even = true; let t1 = performance.now(); do { div++; if (even) { divide(num1, div, num2); add(num2, e); } else { divide(num2, div, num1); add(num1, e); } even = !even; } while (cnt < n); document.getElementById("time").innerText = "Time taken: " + (performance.now() - t1).toFixed(0) + " ms"; print(expText, e); |
Previous page : Numbers, an introduction
Next page : Finding 10000 decimals of pi