source code
/* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* contributed by Denis Gribov
* a translation of the C program contributed by Mr Ledhug
*/
(function main() {
// Int32
let n = +process.argv[2] || 10000,
i = 0,
k = 0,
d = 0,
k2 = 0,
d3 = 0,
d4 = 0;
// BigInt
let tmp1 = 0n, // mpz_init(tmp1)
tmp2 = 0n, // mpz_init(tmp2)
acc = 0n, // mpz_init_set_ui(acc, 0)
den = 1n, // mpz_init_set_ui(den, 1)
num = 1n; // mpz_init_set_ui(num, 1)
const chr_0 = "0".charCodeAt(),
chr_t = "\t".charCodeAt(),
chr_n = "\n".charCodeAt(),
chr_c = ":".charCodeAt();
// preallocated buffer size
let bufsize = (10/*value of pi*/ + 2/*\t:*/ + n.toString().length/*index of slice*/ + 1/*\n*/) * (n / 10)/*line count*/;
// croped buffer size
for (let i = 10, length = 10 ** (Math.log10(n) >>> 0); i < length; i *= 10) {
bufsize -= i - 1;
}
let buf = Buffer.allocUnsafe(bufsize),
bufoffs = 0;
while (i < n) {
k++;
//#region inline nextTerm(k)
k2 = k * 2 + 1;
acc += num * 2n; // mpz_addmul_ui(acc, num, 2)
acc *= BigInt(k2); // mpz_mul_ui(acc, acc, k2)
den *= BigInt(k2); // mpz_mul_ui(den, den, k2)
num *= BigInt(k); // mpz_mul_ui(num, num, k)
//#endregion inline nextTerm(k)
if (num > acc/* mpz_cmp(num, acc) > 0 */) continue;
//#region inline extractDigit(3);
tmp1 = num * 3n; // mpz_mul_ui(tmp1, num, nth);
tmp2 = tmp1 + acc; // mpz_add(tmp2, tmp1, acc);
tmp1 = tmp2 / den; // mpz_tdiv_q(tmp1, tmp2, den);
d3 = Number(tmp1) >>> 0; // mpz_get_ui(tmp1)
//#region inline extractDigit(3);
d = d3;
//#region inline extractDigit(4);
tmp1 = num * 4n; // mpz_mul_ui(tmp1, num, nth);
tmp2 = tmp1 + acc; // mpz_add(tmp2, tmp1, acc);
tmp1 = tmp2 / den; // mpz_tdiv_q(tmp1, tmp2, den);
d4 = Number(tmp1) >>> 0; // mpz_get_ui(tmp1)
//#region inline extractDigit(4);
if (d !== d4) continue;
buf.writeInt8(d + chr_0, bufoffs++);
if (++i % 10 === 0) {
buf.writeInt8(chr_t, bufoffs++);
buf.writeInt8(chr_c, bufoffs++);
let str = i.toString();
buf.write(str, bufoffs, bufoffs += str.length);
buf.writeInt8(chr_n, bufoffs++);
}
//#region inline eliminateDigit(d)
acc -= den * BigInt(d); // mpz_submul_ui(acc, den, d)
acc *= 10n; // mpz_mul_ui(acc, acc, 10)
num *= 10n; // mpz_mul_ui(num, num, 10)
//#endregion inline eliminateDigit(d)
}
process.stdout.write(buf);
})();