32
loading...
This website collects cookies to deliver better user experience
const A = 13591409
const B = 545140134
const C = 640320
function computePQT(n1, n2) {
let m = 0
let PQT = {
P: 0,
Q: 0,
T: 0,
}
if (n1 + 1 === n2) {
PQT.P = n2 * 2 - 1
PQT.P = PQT.P * (n2 * 6 - 1)
PQT.P = PQT.P * (n2 * 6 - 5)
PQT.Q = Math.floor((C * C * C) / 24) * n2 * n2 * n2
PQT.T = (A + B * n2) * PQT.P
if (n2 % 2 === 1) {
PQT.T = -PQT.T
}
} else {
m = Math.floor((n1 + n2) / 2)
let res1 = computePQT(n1, m)
let res2 = computePQT(m, n2)
PQT.P = res1.P * res2.P
PQT.Q = res1.Q * res2.Q
PQT.T = res1.T * res2.Q + res1.P * res2.T
}
return PQT
}
const DIGITS_PER_TERM = 14.1816474627
function computePI(digits) {
if (digits <= 0) {
return '0'
}
const N = Math.floor(digits / DIGITS_PER_TERM) + 1
const PQT = computePQT(0, N)
const PI = (PQT.Q / (12 * PQT.T + 12 * A * PQT.Q)) * Math.pow(C, 3 / 2)
return PI.toFixed(digits)
}
const hrstart = process.hrtime()
const PI = computePI(28)
const hrend = process.hrtime(hrstart)
console.log(PI.toString())
console.info(`Execution time (hr): ${hrend[0]}s ${hrend[1] / 1000000}ms`)
> node index.js
3.1415926535897935600871733186
Execution time (hr): 0s 0.139102ms
3.1415926535897935600871733186
3.1415926535897932384626433832
const A = new BigNumber('13591409')
const B = new BigNumber('545140134')
const C = new BigNumber('640320')
const D = new BigNumber('426880')
const E = new BigNumber('10005')
const DIGITS_PER_TERM = new BigNumber('14.1816474627254776555')
const C3_24 = C.multipliedBy(C).multipliedBy(C).dividedToIntegerBy(24)
function computePI(digits) {
if (digits <= 0) {
return '0'
}
const DIGITS = new BigNumber(digits)
const N = DIGITS.dividedToIntegerBy(DIGITS_PER_TERM).plus(1)
const PREC = DIGITS.multipliedBy(Math.log2(10))
BigNumber.config({
DECIMAL_PLACES: Math.ceil(PREC.toNumber()),
POW_PRECISION: Math.ceil(PREC.toNumber()),
})
const PQT = computePQT(new BigNumber(0), N)
let PI = D.multipliedBy(E.sqrt()).multipliedBy(PQT.Q)
PI = PI.dividedBy(A.multipliedBy(PQT.Q).plus(PQT.T))
return PI.toFixed(digits)
}
function computePQT(n1, n2) {
let m = new BigNumber(0)
let PQT = {
P: new BigNumber(0),
Q: new BigNumber(0),
T: new BigNumber(0),
}
if (n1.plus(1).isEqualTo(n2)) {
PQT.P = n2.multipliedBy(2).minus(1)
PQT.P = PQT.P.multipliedBy(n2.multipliedBy(6).minus(1))
PQT.P = PQT.P.multipliedBy(n2.multipliedBy(6).minus(5))
PQT.Q = C3_24.multipliedBy(n2).multipliedBy(n2).multipliedBy(n2)
PQT.T = A.plus(B.multipliedBy(n2)).multipliedBy(PQT.P)
if (n2.modulo(2).isEqualTo(1)) {
PQT.T = PQT.T.negated()
}
} else {
m = n1.plus(n2).dividedToIntegerBy(2)
let res1 = computePQT(n1, m)
let res2 = computePQT(m, n2)
PQT.P = res1.P.multipliedBy(res2.P)
PQT.Q = res1.Q.multipliedBy(res2.Q)
PQT.T = res1.T.multipliedBy(res2.Q).plus(res1.P.multipliedBy(res2.T))
}
return PQT
}
> node index.js
3.1415926535897932384626433833
Execution time (hr): 0s 3.432017ms
3.1415926535897935600871733186
3.1415926535897932384626433833
3.1415926535897932384626433832
RangeError: Maximum call stack size exceeded
let res1 = await new Promise((resolve) =>
process.nextTick(async () => resolve(await computePQT(n1, m)))
)
let res2 = await new Promise((resolve) =>
process.nextTick(async () => resolve(await computePQT(m, n2)))
)