TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

20M digits of pi in 1 minute using Julia

109 pointsby juliusgeoabout 2 years ago

11 comments

queuebertabout 2 years ago
I&#x27;ve found Julia to be an excellent language for Project Euler [1]. Besides the speed, you can use Unicode identifiers, so the solution can closer follow the math.<p>1. <a href="https:&#x2F;&#x2F;projecteuler.net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;projecteuler.net&#x2F;</a>
评论 #35017144 未加载
Someoneabout 2 years ago
Nitpick: is that really computing 20M digits correctly? Reading the code gives me the impression that this does all float computations in 20 million digits (or 20 million-ish? If BigFloat internally is binary, it might use a bit more), but I would expect that you need to compute using a few more digits to avoid rounding errors in the last digits.<p>Edit: also, reading <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;67919533" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;67919533</a>, it seems you can do<p><pre><code> BigFloat(π, precision=20000000) </code></pre> How does that perform?
评论 #35020293 未加载
评论 #35020965 未加载
jiggawattsabout 2 years ago
In Wolfram Mathematica on my desktop PC with a 7 year old processor:<p><pre><code> Timing[N[\[Pi], 20000000];] {10.1094, Null} </code></pre> On a fairly recent laptop CPU I got 7.6 seconds.<p>A fair ways to go still!
评论 #35021510 未加载
评论 #35019859 未加载
评论 #35020794 未加载
评论 #35020854 未加载
adgjlsfhk1about 2 years ago
This is pretty cool. One minor gripe is that there were a lot of useless type asserts cluttering things up. every usage of ::BigFloat here wasn&#x27;t needed.
评论 #35017252 未加载
Temporary_31337about 2 years ago
Just wanted to mention that SuperPi - the benchmark that is very widely used for at least a decade for measuring PC &#x2F; CPU single threaded performance uses the same algorithm so not really obscure. That said, multi threaded algorithms make more sense for modern multi core computer architectures, something like this for example:<p><a href="http:&#x2F;&#x2F;www.numberworld.org&#x2F;y-cruncher&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.numberworld.org&#x2F;y-cruncher&#x2F;</a>
评论 #35018966 未加载
goldenkeyabout 2 years ago
For reference: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Gauss%E2%80%93Legendre_quadrature" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Gauss%E2%80%93Legendre_quadrat...</a>
bakulabout 2 years ago
The following implementation of the Chudnovsky algorithm takes about the same time in Gambit-Scheme on M1 Mac for 20M digits (and allocates 16GB):<p><pre><code> (define (pi digits) (let* ((A 13591409) (B 545140134) (C 640320) (C^3&#x2F;24 (quotient (expt 640320 3) 24)) (D 12)) (define (-1^n*g n g) (if (odd? n) (- g) g)) (define (split m n) (if (= 1 (- n m)) (let* ((6n (* 6 n)) (g (* (- 6n 5) (- (+ n n) 1) (- 6n 1)))) (list g (* C^3&#x2F;24 (expt n 3)) (* (-1^n*g n g) (+ (* n B) A)))) (let* ((mid (quotient (+ m n) 2)) (gpq1 (split m mid)) (gpq2 (split mid n)) (g1 (car gpq1)) (p1 (cadr gpq1)) (q1 (caddr gpq1)) (g2 (car gpq2)) (p2 (cadr gpq2)) (q2 (caddr gpq2))) (list (* g1 g2) (* p1 p2) (+ (* q1 p2) (* q2 g1)))))) (let* ((num-terms (inexact-&gt;exact (floor (+ 2 (&#x2F; digits 14.181647462))))) (sqrt-C (integer-sqrt (* C (expt 100 digits)))) (gpq (split 0 num-terms)) (g (car gpq)) (p (cadr gpq)) (q (caddr gpq))) (quotient (* p C sqrt-C) (* D (+ q (* p A))))))) </code></pre> I am surprised it is much slower in Julia (as per what is noted in the gist).
评论 #35021829 未加载
IncRndabout 2 years ago
Here is a video [1] and associated java source code [2] from pi day in 2018 to generate 1M (or more) digits of pi using the same Gauss-Legendre algorithm.<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=8RONJPOgZjw">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=8RONJPOgZjw</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;R-e-t-u-r-n-N-u-l-l&#x2F;Java-Algorithms&#x2F;blob&#x2F;master&#x2F;ApproxPi.java">https:&#x2F;&#x2F;github.com&#x2F;R-e-t-u-r-n-N-u-l-l&#x2F;Java-Algorithms&#x2F;blob&#x2F;...</a>
评论 #35017267 未加载
评论 #35017564 未加载
joseftexasabout 2 years ago
Isn&#x27;t this is more hardware dependent? A 2012 Intel notebook vs a 2022 AMD laptop would yield different results with same code. Also a well tune assembly can do these way faster. A python wrapped with assembly functions and run on a machine 6ghz Intel machine with ramdisk would also outperform this. I always feel this kind of claim is not helpful in day to day usage.
评论 #35018042 未加载
tiffanyhabout 2 years ago
Is 1-minute fast?
评论 #35017944 未加载
评论 #35017755 未加载
评论 #35017292 未加载
评论 #35017844 未加载
boredemployeeabout 2 years ago
genuine question: would that amount of digits be useful for any application?
评论 #35017847 未加载
评论 #35017888 未加载
评论 #35018516 未加载