Skip to content

Latest commit

 

History

History
85 lines (54 loc) · 1.58 KB

README.md

File metadata and controls

85 lines (54 loc) · 1.58 KB

pi-generator

Pausable/resumable pi digits generator.

See the demo: https://betafcc.github.io/pi-generator

This module was made to demonstrate the new native BigInt for JS.

Install

For node version > 10.x and latest Chrome:

npm install pi-generator

For older node and browsers, this equivalent package uses big-integer instead of the native BigInt:

npm install pi-generator-legacy

Usage

Print pi digits forever:

import {piDigits} from 'pi-generator'

for (const digit of piDigits())
  console.log(digit)

// 3
// 1
// 4
// 1
// 5
// ...

Take some digits:

import {takeDigits} from 'pi-generator'

// generates first 5 digits
const [digits, env] = takeDigits(5)

console.log(digits) // '31415'

Note there is an extra returned value, the 'environment' of the calculation:

console.log(env)
// { q: 622702080000000n,
//   r: -434409300281250n,
//   t: 213458046676875n,
//   k: 14n,
//   n: 6n,
//   l: 29n }

This is what makes possible to resume the digits expansion:

// generates the next 5 digits, continuing from 'env'
const [moreDigits, nextEnv] = takeDigits(5, env)

console.log(moreDigits) // '92653'

The stringify and parse functions are provided to save the calculation environment:

import {takeDigits, stringifyEnv, parseEnv} from 'pi-generator'

const [digits, env] = takeDigits(100)

// can store this one in a json file or wtv
const envString = stringifyEnv(env)


// can parse and resume the expansion
takeDigits(100, parseEnv(envString))