Live data from Hacker News

A Million Digits of Pi in 9 Lines of JavaScript

ajennings.net

61–70 of 86 posts

Re: A Million Digits of Pi in 9 Lines of JavaScript

#61
post #9

For those of us behind work proxies that dumbly think this site is "domain parking" or worse: http://archive.is/hUo6Q

archive.is blocks resolution from the cloudflare recursive resolver. please consider using web.archive.org which doesn't block anyone.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#65
post #46

His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits. But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress. Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

It's not just the comparisons, it's the additions, multiplications and divisions too. When you enter 10,000, each iteration of the loop is working with numbers 10,000 digits long. When you enter 100,000, each iteration is working with numbers 100,000 digits long, which I imagine makes every operation slower.

A number fills the same space in your registers regardless of size, up until the max size, at which point it must be split into multiple operations, and that's when it takes longer.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#68
post #60

let y=3n*(10n**1000020n); const f=(i,x,p)=>{(x>0)?f(i+2n,x*i/((i+1n)*4n),p+x/(i+2n)):p/(10n**20n)} console.log(f(1n,y/8n,y)); Not sure if I can golf it anymore

That version already doesn’t work… once you fix the arrow function, there’s also the issue that most engines today don’t support proper tail calls, so nothing recursive will be portable. (But if it did, you could save a lot of characters by dropping unnecessary parentheses, expanding (i+1n)×4n to 4n×i+4n, replacing the const with a comma, removing semicolons…)

Re: A Million Digits of Pi in 9 Lines of JavaScript

#69
post #60

let y=3n*(10n**1000020n); const f=(i,x,p)=>{(x>0)?f(i+2n,x*i/((i+1n)*4n),p+x/(i+2n)):p/(10n**20n)} console.log(f(1n,y/8n,y)); Not sure if I can golf it anymore

That version already doesn’t work… once you fix the arrow function, there’s also the issue that most engines today don’t support proper tail calls, so nothing recursive will be portable. (But if it did, you could save a lot of characters by dropping unnecessary parentheses, expanding (i+1n)×4n to 4n×i+4n, replacing the const with a comma, removing semicolons…)

this is my perl golf version of the same series. You can probably can do something similar with js:

map$l+=(-1)$_/(1-$_2)4,1..;die$l

Re: A Million Digits of Pi in 9 Lines of JavaScript

#70

Earlier quoted context omitted.

That version already doesn’t work… once you fix the arrow function, there’s also the issue that most engines today don’t support proper tail calls, so nothing recursive will be portable. (But if it did, you could save a lot of characters by dropping unnecessary parentheses, expanding (i+1n)×4n to 4n×i+4n, replacing the const with a comma, removing semicolons…)

this is my perl golf version of the same series. You can probably can do something similar with js: map$l+=(-1) $_/(1-$_ 2) 4,1.. ;die$l

Doesn't work for me [0]. Perhaps HN messed it up somehow?

[0]: https://tio.run/##K0gtyjH9/z83sUAlR9tWQ9dQUyVeX8NQVyXeSNNEx1...

Post reply on HN