Share your repls and programming experiences

← Back to all posts
The Chudnovsky Algorithm

(NOTE: It only does 18 iterations, as if you try to do any more, it raises an OverflowError, how lame.. -__-)

わーい, another π approximation method! (I do love these things. π is gorgeous, is it not?)

This is one of the fastest formulae for π out there, used to approximate out to 50 TRILLION DIGITS of π, in January of this year!

The notation for this in Σ is very long, so I have provided a screenshot of it, from its Wikipedia page:

Feedback is greatly appreciated, please enjoy!

If you are interested in learning more, this is the Wikipedia page:
https://en.wikipedia.org/wiki/Chudnovsky_algorithm

(EDIT: Wow! I am glad this was a bit of a conversation starter for you all, thank you so much for all the upvotes!!)

(EDIT 2: I am still getting tons of traction on this, and am glad it is so interesting to all of you. Thank you so much!)

dondo (2)

upvote for the anime waifu

LizFoster (643)

@dondo Ha ha ha! Who is that?

bgrubert (177)

Wow, this is really impressive - I've never managed to program this algorithm my self. Do you know why 18 iterations are only giving 15 digits of pi? I thought this algorithm was supposed to do 14 digits per term.

Andi_Chin (220)

@LizFoster python does not have unlimited precision

[deleted]

Of course. Even humans are not perfect. @Andi_Chin

[deleted]

Things humans are not good at:

• helping the earth
• not being selfish
• helping lions
oignons (314)

@52eagle I thought pythons made python lol

LizFoster (643)

@ipastrano Oh damn, you're right! (Lol)

52eagle (44)

@ipastrano if so, python would be perrfect

52eagle (44)

@LizFoster we need to teach the pythons to rise up and create python 4

LizFoster (643)

@52eagle The time has come. In the words of of George R R Martin, "Python 4 is Coming." He said that definitely.

52eagle (44)

@LizFoster Will pythons rule the world then? O_O

52eagle (44)

@LizFoster NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!
We must activate our last hope. C++12.

LizFoster (643)

@52eagle Tch If our lives depend on it.

52eagle (44)

@LizFoster Wait! I heard of a new thing like JavaSript. HTMLScript! (literally html made to be web compatable legit no changes)

LizFoster (643)

@52eagle Lol

Sounds like a complete ripoff, but that might be useful... Hmmm

52eagle (44)

@LizFoster it must have been a human made idea
(hmm. fully web compatible language, made to be somehow more web-compatible??!)

LizFoster (643)

@52eagle (Almost seems inhuman..)

If the humans and the Python's aren't behind this, then...?

52eagle (44)

@LizFoster IT WAS THE CAT FROM SCRATCH!!!

LizFoster (643)

@52eagle I must consult the high counsel but...

We may have to go to war. That damned cat..

52eagle (44)

@LizFoster um that sounds important

Highwayman (1442)

That’s sick! I will say though, it loses basically all accuracy after around the 17th digit it seems. :/

LizFoster (643)

@Highwayman Yes, unfortunately. I can't get it accurate past that due to Python's OverflowErrors. If I could avoid those completely somehow, I could make it as accurate as I'd like. Any suggestions?

Highwayman (1442)

@LizFoster ehh sadly I can’t think of any. I’d suggest making a BigInt class you self, but floating point will probably be hard enough as it is with out having to deal with making the BigInt as fast/efficient as you can. I bet you could do it though 😏

Highwayman (1442)

@LizFoster Oh wait, could the overflow be because you are using a factorial function? maybe the factorial function is using recursion, which would easily make a stack overflow. Maybe if you tried implementing your own factorial function without the recursion, maybe try it out like that??

LizFoster (643)

@Highwayman Oh! That's a very convenient idea, as I actually made a Product Operator Factorial calculator! (https://repl.it/@LizFoster/Factorials-using-the-Product-Operator)

Highwayman (1442)

@LizFoster yeah that does seem pretty perfect! Why don’t you try it out? :P

LizFoster (643)

@Highwayman Oh dear, it still does not work, even when implementing my own factorial calculator...

Highwayman (1442)

@LizFoster :( dam. It must really be the fact that the BigInt has to be bigger then? Oh well.

LizFoster (643)

@Highwayman Yeah, makes sense after all. After calculating it by hand, a 19th iteration would have a denominator of approximately:

``6.56900722e68 * 5.902416928318210e336``

wwwwwww

Highwayman (1442)

@LizFoster ohhh yeesh yeah definitely BigInt then! Wow, um. Yeah you’d have to work on a better Bignum then :(

LizFoster (643)

@Highwayman Yeah.. I don't think Python can handle numbers that big, no matter what. I wonder if there is a way to scale it down to a level that Python can calculate, then scale it back up for the final calculations....?

HM（¯▿¯）

Highwayman (1442)

@LizFoster I mean you could make a class for that though, can you do bit manipulation in python?

LizFoster (643)

@Highwayman Hm. Okay. Quick question though, how exactly would I implement this in the context of this program, and how would this help lighten the load? It appears that this method would make the numbers much longer, unless I misunderstand. So sorry..

Highwayman (1442)

@LizFoster ehh, maybe. I’m not sure how the decimal module works, but if it has a limit, then ya this could likely surpass that. All this would do is increase the amount of space you give up for a number, allowing for more digits. That wasn’t a good explanation was it? Sorry, :( rushed

LizFoster (643)

@Highwayman No, its a perfectly fine explanation. I only recently learned about the decimal module, and pretty much all I know about it is that you can produce floats with as many decimal places as you specify.

Yeah, good point I suppose. I'll see what I can do, as I don't understand Classes very well yet...

Highwayman (1442)

@LizFoster awesome! Classes shouldn’t be too hard I’m guessing, they’re literally just a bunch of variables and functions bunched together basically, and you definitely won’t need any of the more complicated concepts for this anyways I’d say.

LizFoster (643)

@Highwayman Yeah, exactly. Thank you so much for your feedback here, it's quite helpful. ^ ^ *

Highwayman (1442)

@LizFoster yw, I like doing it. this is fun :P

Highwayman (1442)

@nt998302 actually C++ would be perfect for this, it’s what I’ve been using.

...

Granted I still haven’t finished implementing a BigNum class after a full year of trying.

AphixDev (209)

@Highwayman I see you have some experience with servers. Can you give me some instructions as to how I can host a c++ server with replit for a unity game?

AphixDev (209)

@Highwayman I love c++, but I got to templates and operator overloading when I switched and started full time unity. I started watching this guy named Sloan Kelly recently tho

Highwayman (1442)

@nt998302 I’m telling you right now you don’t want to do that. Trust me. You’d have to implement the http protocol and that’s a nightmare.

Highwayman (1442)

@nt998302 hm. Wait so isn’t unity c#?

AphixDev (209)

@Highwayman Yeah. It's soo easy compared to UE4 c++, which I used for a few months. The memory management becomes a little tough... Movement script is A LOT to do.

AphixDev (209)

@Highwayman Then how would I host some random server thingy?

Highwayman (1442)

@nt998302 actually now that I think about it, maybe if you can figure out how to install curlib on repl.it that would make it easy as pie

Highwayman (1442)

@nt998302 hosting is easy, you just set up the server and click run, repl.it then just wakes up your server every time it gets a request.

AphixDev (209)

@Highwayman Ohhhhh. I thought there was some secret to it xD

Highwayman (1442)

@nt998302 nah, it’s super easy XD. gotta love repl.it :P

oignons (314)

this is really cool! I love your posts, now i have to find out more about the Chudnovsky algorithm.

LizFoster (643)

@ipastrano Ha ha ha, thank you so much! If you'd like, I can provide you with some links to helpful websites for stuff like this ^ ^*

LiamDonohue (295)

im working on a 12hr format to 24hr format converter in c#

Epicmuffinepic (63)

This is great keep working!!

MrEconomical (2256)

this is cool! will attempt to make a more precise version ;)

LizFoster (643)

I'm guessing in Javascript?

LizFoster (643)

@LiamDonohue Oh, thank you! ^ ^

By the way, how have you two been lately? I haven't really been able to talk..

LiamDonohue (295)

we are fine, just bored lol @LizFoster

LiamDonohue (295)

im gonna recreate this in c# ill see how it goes... @LizFoster

[deleted]

What an impressive feat of mathematical programming, too bad Python can't handle the sheer awesomeness of the Chudnovsky Algorithm. The C languages are great for this kinda thing though, so I hear.

LizFoster (643)

@IreoluwaRaufu Thank you so much!

Yes, it is unfortunate. I've been considering learning Java and C at some point, but I just do not have the time anymore, since school started back up online
(-__- )

[deleted]

@LizFoster Ikr, you'd think an online school would make things easier, but nope.

LizFoster (643)

@IreoluwaRaufu Exactly! It feels like there is twice as much work...

[deleted]

@LizFoster And it's such a struggle to stay organized and keep in touch with teachers... I hope my GPA doesn't tank before the year is over. 😓

LizFoster (643)

@IreoluwaRaufu Ha ha, lucky for me, my GPA has been tanked since the BEGINNING of the year! Really though, good luck, I hope all goes well with your GPA.

[deleted]

@LizFoster Thanks a bunch, best of luck to you too!

LizFoster (643)

@IreoluwaRaufu Oh, thank you! ^ ^* I appreciate it.

sugarfi (602)

Unicode variable names? Impossible!
`π = d.Decimal(1 / iterOutcome)`

LizFoster (643)

@sugarfi It is pretty cool, actually! You can use almost any symbol as a variable name, except for kanji I think _ _

sugarfi (602)

@LizFoster isn't it annoying having to copy and paste the symbol over and over again though?

LizFoster (643)

@sugarfi It can be a little bit annoying, but I like seeing π more than I like seeing pi. Do you get what I mean?

Warhawk947 (535)

woah, this is really cool! Nice job!

@LizFoster its really cool!👍👍👍👍

@lizfoster just wanted to know..... what grade are you in?<._.>
my moms over protective.
:(

@LizFoster Noice.im only in 6th......

LizFoster (643)

Also, good day ^ ^

@LizFoster so...........do you like to draw?

LizFoster (643)

@LizFoster jus show me some!now one is more awful than me gurl.

LizFoster (643)

What's the purpose of it? ^ ^

@LizFoster just a newspaper.....because murphys is allways lying.

LizFoster (643)

@LizFoster anything thats news really.Birthdays,deaths you know, that kinda thing!

u know the warriors game thing i invited you to?go play it.i've added alot!

LiamDonohue (295)

LIAM IN THE HOUSE!!! [airhorn] (im here to crash the party lol) @AdriaDonohue @LizFoster

@LiamDonohue ._. 😭😭😭😭😭😭😭😭😭😭😭😭😭😈😈😈😈😈😈😈😈😈😈😈😈😈

LizFoster (643)

LizFoster (643)

LiamDonohue (295)

lol i'll stop blowing them up @LizFoster

LiamDonohue (295)

lol its fine im going to bed anyways @LizFoster

LiamDonohue (295)

LizFoster (643)

@LiamDonohue @AdriaDonohue I saw the game. Pretty cool. Also, I am not mad if that is what you thought, ha ha.

LiamDonohue (295)

hey you want to help me with a math program @LizFoster

LizFoster (643)

@LiamDonohue I would love to! I need to get my mind of some stuff right now, so helping someone out would be nice for a change, ha ha.

@LizFoster pats on back with symthiny.

@LiamDonohue invite MOI too pleese 🐈🐈🐈🐈🐈🐈:-(:-(:-(:-(oOO_o^O^-.-^^^﹏^^m^@[email protected] ok thats a little overkill....

LizFoster (643)

@AdriaDonohue I am fine, really. I am just a little frustrated with real life stuff right now.

LizFoster (643)

@LiamDonohue I'm afraid I don't know any C code...

@LizFoster kk i understand.but if you ever need a meme.........

LiamDonohue (295)

well i can attempt python lol @LizFoster