下面我们使用loop 和recur 让你的函数无限循环。我没有使用setTimeout,而是尽可能快地重复出现,而是为每个1000间隔输出一个结果x -
const recur = (...values) =>
({ recur, values })
const loop = f =>
{ let acc = f ()
while (acc && acc.recur === recur)
acc = f (...acc.values)
return acc
}
const calculatePi = (limit = Infinity) =>
loop // loop our function ...
( (total = 0, x = 1, addSubBool = false) =>
{ if (x > limit) // stop condition
return total
if (x % 1e3 === 1) // display progress as we go
console.log(x, total, total - Math.PI)
if (total === Math.PI) // solution found
return total
if (addSubBool)
return recur // recur with ...
( total - 4 / x // next total
, x + 2 // next x
, false // next addSubBool
)
else
return recur // recur with ...
( total + 4 / x // next total
, x + 2 // next x
, true // next addSubBool
)
}
)
console.log(calculatePi(1e7))
如您所见,此方法需要很长时间才能收敛到答案。即使在一千万 (10M) x 之后,我们仍然只计算了 6 个精度点 -
x total diff
...
9997001 3.1415924535297624 -2.0006003076389334e-7
9998001 3.1415924535497695 -2.0004002365681117e-7
9999001 3.141592453569776 -2.0002001699381822e-7
另一种方法是将precision 作为calculatePi 的输入。我们不会被任意的x 限制,而是继续计算直到达到特定的精度。出于演示目的,此函数还返回 x,因此我们可以看到 x 在达到所需精度之前必须达到多大 -
const calculatePi = (precision = 1e5) =>
loop
( (total = 0, x = 1, addSubBool = false) =>
{ if (total * precision >> 0 === Math.PI * precision >> 0)
return [ total, x ]
if (addSubBool)
return recur
( total - 4 / x
, x + 2
, false
)
else
return recur
( total + 4 / x
, x + 2
, true
)
}
)
如您所见,x 超过 3700 万,达到小数点后 7 位的精度 -
console .log
( calculatePi (1e2)
// [ 3.14999586659347, 239 ]
, calculatePi (1e3)
// [ 3.141000236580159, 3377 ]
, calculatePi (1e4)
// [ 3.1415000095284658, 21589 ]
, calculatePi (1e5)
// [ 3.141599999994786, 272243 ]
, calculatePi (1e7)
// [ 3.1415926000000005, 37320609 ]
)
展开下面的 sn-p 以在浏览器中验证结果 -
const recur = (...values) =>
({ recur, values })
const loop = f =>
{ let acc = f ()
while (acc && acc.recur === recur)
acc = f (...acc.values)
return acc
}
const calculatePi = (precision = 1e5) =>
loop
( (total = 0, x = 1, addSubBool = false) =>
{ if (total * precision >> 0 === Math.PI * precision >> 0)
return [ total, x ]
if (addSubBool)
return recur
( total - 4 / x
, x + 2
, false
)
else
return recur
( total + 4 / x
, x + 2
, true
)
}
)
console .log
( calculatePi (1e2)
// [ 3.14999586659347, 239 ]
, calculatePi (1e3)
// [ 3.141000236580159, 3377 ]
, calculatePi (1e4)
// [ 3.1415000095284658, 21589 ]
, calculatePi (1e5)
// [ 3.141599999994786, 272243 ]
, calculatePi (1e7)
// [ 3.1415926000000005, 37320609 ]
)
最后,在计算 pi 时检查 Math.PI 没有多大意义;我想整个目标是计算一个我们假装不知道的数字。为此,我们从一些guess 开始,然后测量它与total 之间的差异。如果猜测在指定的容差范围内,则返回猜测 -
const calculatePi = (precision = 1e5) =>
loop
// guess starts at 1
( (guess = 1, total = 0, x = 1, addSubBool = false) =>
{ if (Math .abs (guess - total) * precision < 1)
return [ guess, x ]
if (addSubBool)
return recur // recur with ...
( total // next guess
, total - 4 / x // next total
, x + 2 // next x
, false // next addSubBool
)
else
return recur // recur with ...
( total // next guess
, total + 4 / x // next total
, x + 2 // next x
, true // next addSubBool
)
}
)
我们可以看到它按预期工作。诚然,我对输入精度与计算所需的 x 之间的相关性感到惊讶 -
console .log
( calculatePi (1e2)
// [ 3.136592684838816, 403 ]
, calculatePi (1e3)
// [ 3.1410926536210413, 4003 ]
, calculatePi (1e4)
// [ 3.1415426535898248, 40003 ]
, calculatePi (1e5)
// [ 3.1415876535897618, 400003 ]
, calculatePi (1e7)
// [ 3.141592603589817, 40000003 ]
)
展开下面的 sn-p 以在浏览器中验证结果 -
const recur = (...values) =>
({ recur, values })
const loop = f =>
{ let acc = f ()
while (acc && acc.recur === recur)
acc = f (...acc.values)
return acc
}
const calculatePi = (precision = 1e5) =>
loop
// guess starts at 1
( (guess = 1, total = 0, x = 1, addSubBool = false) =>
{ if (Math .abs (guess - total) * precision < 1)
return [ guess, x ]
if (addSubBool)
return recur // recur with ...
( total // next guess
, total - 4 / x // next total
, x + 2 // next x
, false // next addSubBool
)
else
return recur // recur with ...
( total // next guess
, total + 4 / x // next total
, x + 2 // next x
, true // next addSubBool
)
}
)
console .log
( calculatePi (1e2)
// [ 3.136592684838816, 403 ]
, calculatePi (1e3)
// [ 3.1410926536210413, 4003 ]
, calculatePi (1e4)
// [ 3.1415426535898248, 40003 ]
, calculatePi (1e5)
// [ 3.1415876535897618, 400003 ]
, calculatePi (1e7)
// [ 3.141592603589817, 40000003 ]
)