TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

A Million Digits of Pi in 9 Lines of JavaScript

172 点作者 ajennings超过 5 年前

21 条评论

dmurray超过 5 年前
&gt; this simple one still converges at about 0.6 decimal digits per term.<p>Quick proof of this: as the number of terms <i>n</i> in the sum goes to infinity, the ratio of each term to the previous one is approximately 1&#x2F;4 - the first factor contributes <i>m&#x2F;(m+1)</i>, the second <i>q&#x2F;(q+2)</i> for some <i>m</i> and <i>q</i> that go to infinity along with <i>n</i>, the third contributes 1&#x2F;4.<p>If we counted base 4, then the value of each digit would be on average 1&#x2F;4 of the previous one, certainly for a normal number like pi. But we count base 10, so we get <i>log_10 4</i> decimal digits every time we get one base-four digit. Which is very close to 0.6.
评论 #20957163 未加载
jrochkind1超过 5 年前
His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits.<p>But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress.<p>Hmm. Have to think about that one. Just cause it&#x27;s asking JS to do comparisons of much larger numbers?
评论 #20957651 未加载
评论 #20957539 未加载
评论 #20957609 未加载
评论 #20957359 未加载
tombert超过 5 年前
Wow, I wonder why Firefox is so much slower? Quantum seems pretty zippy overall, but maybe that&#x27;s largely due to fast rendering speeds?<p>Does anyone on the Spidermonkey team have some insight?
评论 #20955409 未加载
评论 #20955082 未加载
评论 #20955415 未加载
mourner超过 5 年前
Alternatively, you can generate Pi digits one by one in a streaming way: <a href="https:&#x2F;&#x2F;observablehq.com&#x2F;@mourner&#x2F;calculating-pi-digits" rel="nofollow">https:&#x2F;&#x2F;observablehq.com&#x2F;@mourner&#x2F;calculating-pi-digits</a>
wasnthere超过 5 年前
JS Error `No identifiers allowed directly after numeric literal` when running <a href="http:&#x2F;&#x2F;ajennings.net&#x2F;pi.html" rel="nofollow">http:&#x2F;&#x2F;ajennings.net&#x2F;pi.html</a> on Mac OS Mojave 10.14.6, Safari 12.1.2 (14607.3.9)
评论 #20955702 未加载
评论 #20959614 未加载
phonebucket超过 5 年前
A fun related thread on math stackexchange which has several examples of series which converge quickly to pi: <a href="https:&#x2F;&#x2F;math.stackexchange.com&#x2F;questions&#x2F;14113&#x2F;series-that-converge-to-pi-quickly" rel="nofollow">https:&#x2F;&#x2F;math.stackexchange.com&#x2F;questions&#x2F;14113&#x2F;series-that-c...</a><p>edit: There also some nice formulae for quick convergence in this article: <a href="https:&#x2F;&#x2F;julialang.org&#x2F;blog&#x2F;2017&#x2F;03&#x2F;piday" rel="nofollow">https:&#x2F;&#x2F;julialang.org&#x2F;blog&#x2F;2017&#x2F;03&#x2F;piday</a>
AdmiralAsshat超过 5 年前
Can someone explain the logic behind the evolution of the fractions at each stage? I see that (1&#x2F;4) becomes (1&#x2F;4^2) and (1&#x2F;4^3), but it&#x27;s not obvious to me how (1&#x2F;3)-&gt;(1&#x2F;5)-&gt;(1&#x2F;7) flows (odds? primes?), or (1&#x2F;2) -&gt; (13&#x2F;24) -&gt; (135&#x2F;246).<p>EDIT: I understand now, the numerator on the first term is ascending odds and the denominator is ascending evens. Thanks for everyone&#x27;s help!
评论 #20956393 未加载
评论 #20956306 未加载
评论 #20956477 未加载
评论 #20956289 未加载
bakul超过 5 年前
&gt; this simple one still converges at about 0.6 decimal digits per term.<p>The Chudnovsky brothers’ algorithm computes 14.18... digits per term. Its implementation in Scheme is only about a couple dozen lines of code. It computes a million pi digits in about 17.5 seconds on raspberry pi 4 in Gambit Scheme (57 seconds on the original raspberry pi, IIRC).
52-6F-62超过 5 年前
For those of us behind work proxies that dumbly think this site is &quot;domain parking&quot; or worse:<p><a href="http:&#x2F;&#x2F;archive.is&#x2F;hUo6Q" rel="nofollow">http:&#x2F;&#x2F;archive.is&#x2F;hUo6Q</a>
评论 #20956570 未加载
评论 #20959031 未加载
LeoPanthera超过 5 年前
On any standard unix system with bc installed - it&#x27;s preinstalled on most of them, you can calculate pi to $n digits using bc:<p>bc -l &lt;&lt;&lt; &quot;scale=$n; 4*a(1)&quot;
评论 #20956465 未加载
评论 #20955741 未加载
评论 #20957658 未加载
russellbeattie超过 5 年前
I was going to make a joke about just writing Math.PI.toFixed(10 * * 6), but it turns out that Number.toFixed() only supports up to 100 places AND Math.PI stops at the 50th place. Still amusing to me. Also, BigInt numbers can&#x27;t be combined with non BigInt without conversion. I haven&#x27;t had cause to use them yet, so I didn&#x27;t know that.<p>Learned three new things making a dumb HN joke... not bad!
userbinator超过 5 年前
I thought it would be the &quot;spigot&quot; algorithm, which is based on Bellard&#x27;s formula (yes, <i>that</i> Bellard) and yields similarly small programs:<p><a href="http:&#x2F;&#x2F;numbers.computation.free.fr&#x2F;Constants&#x2F;TinyPrograms&#x2F;tinycodes.html" rel="nofollow">http:&#x2F;&#x2F;numbers.computation.free.fr&#x2F;Constants&#x2F;TinyPrograms&#x2F;ti...</a>
评论 #20959199 未加载
sp332超过 5 年前
This also takes a lot of RAM. Keep an eye on it during longer executions or the swapping could make your box pretty unusable.
评论 #20956724 未加载
评论 #20956532 未加载
andig超过 5 年前
I still can&#x27;t work out how the formula shown translates to the code given. Any hints?
adossi超过 5 年前
I&#x27;m interested to see how the computation time would progress with the recent V8 memory enhancements.
foxes超过 5 年前
<p><pre><code> let y=3n*(10n**1000020n); const f=(i,x,p)=&gt;{(x&gt;0)?f(i+2n,x*i&#x2F;((i+1n)*4n),p+x&#x2F;(i+2n)):p&#x2F;(10n**20n)} console.log(f(1n,y&#x2F;8n,y)); </code></pre> Not sure if I can golf it anymore
评论 #20959782 未加载
ilovepeppapig超过 5 年前
It will be much faster if you don&#x27;t update the HEX values all the time (they are not that interesting anyway).
评论 #20962649 未加载
hervature超过 5 年前
What is the actual equation? They just list the first couple of terms.
评论 #20956871 未加载
评论 #20955112 未加载
评论 #20955123 未加载
dlojudice超过 5 年前
Is there a BigDecimal, Money, or similar coming to JS?
评论 #20958404 未加载
paulpauper超过 5 年前
infinite series ..an amazing discovery
craftinator超过 5 年前
wget &quot;<a href="https:&#x2F;&#x2F;www.piday.org&#x2F;million&#x2F;&quot;" rel="nofollow">https:&#x2F;&#x2F;www.piday.org&#x2F;million&#x2F;&quot;</a><p>Got you beat, js.
评论 #21053605 未加载