Created
March 27, 2022 15:46
-
-
Save doxyf/a9ed833bc94d655cee1ba72ec7af60f3 to your computer and use it in GitHub Desktop.
PI digits generator in NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const process = require('process'); | |
const fs = require('fs'); | |
let digits = process.argv[2] ?? 9999 | |
let i = 1n; | |
x = 3n * (10n ** (BigInt(digits) + 20n)); | |
let pi = x; | |
let pistr16; | |
let started = new Date(); | |
let digitsCounted; | |
let seconds; | |
while (x > 0) { | |
x = x * i / ((i + 1n) * 4n); | |
pi += x / (i + 2n); | |
i += 2n; | |
pistr16 = pi.toString(16); | |
digitsCounted = Math.round(((pistr16.length - x.toString(16).length) * 1.20412) - 20); | |
seconds = (((Date.now() - started.getTime())/1000)); | |
console.log('Calculated ' + digitsCounted.toString() + ' digits out of ' + digits + ' in ' + Math.floor(seconds).toString()+ ' seconds. Hashrate: ' + (digitsCounted/seconds).toString() + ' D/S'); | |
process.stdout.clearLine(1); | |
process.stdout.moveCursor(0, -1); | |
}; | |
let pee = (pi / (10n ** 20n)); | |
//console.log('3.'+pee.toString().slice(1)); | |
process.stdout.clearLine(1); | |
process.stdout.moveCursor(0, -1); | |
console.log('\nCalculated ' + digitsCounted.toString() +' digits of PI in ' + seconds.toString()+ ' seconds. Hashrate: ' + (digitsCounted/seconds).toString() + ' D/S'); | |
fs.writeFileSync('./pi.txt', '3.'+pee.toString().slice(1), 'utf-8'); | |
console.log('\nPI was written to "./pi.txt" file.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment