Share your repls and programming experiences

← Back to all posts
The Chudnovsky Algorithm
LizFoster (592)

(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!)

Commentshotnewtop
dondo (2)

upvote for the anime waifu

LizFoster (592)

@dondo Ha ha ha! Who is that?

bgrubert (140)

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 (158)

@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

@LizFoster

ipastrano (244)

@52eagle I thought pythons made python lol

LizFoster (592)

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

52eagle (45)

@ipastrano if so, python would be perrfect

52eagle (45)

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

LizFoster (592)

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

52eagle (45)

@LizFoster Will pythons rule the world then? O_O

52eagle (45)

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

LizFoster (592)

@52eagle Tch If our lives depend on it.

52eagle (45)

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

LizFoster (592)

@52eagle Lol

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

52eagle (45)

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

LizFoster (592)

@52eagle (Almost seems inhuman..)

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

52eagle (45)

@LizFoster IT WAS THE CAT FROM SCRATCH!!!

LizFoster (592)

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

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

52eagle (45)

@LizFoster um that sounds important

Highwayman (1319)

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

LizFoster (592)

@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 (1319)

@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 (1319)

@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 (592)

@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 (1319)

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

LizFoster (592)

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

Highwayman (1319)

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

LizFoster (592)

@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 (1319)

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

LizFoster (592)

@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 (1319)

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

LizFoster (592)

@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 (1319)

@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 (592)

@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 (1319)

@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 (592)

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

Highwayman (1319)

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

Highwayman (1319)

@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.

nt998302 (160)

@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?

nt998302 (160)

@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 (1319)

@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 (1319)

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

nt998302 (160)

@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.

nt998302 (160)

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

Highwayman (1319)

@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 (1319)

@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.

nt998302 (160)

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

Highwayman (1319)

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

ipastrano (244)

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

LizFoster (592)

@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 (279)

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

gameplayrawesom (127)

This is great keep working!!

MrEconomical (2197)

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

LizFoster (592)

@MrEconomical -_-

I'm guessing in Javascript?

LizFoster (592)

@LiamDonohue Oh, thank you! ^ ^

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

LiamDonohue (279)

we are fine, just bored lol @LizFoster

LiamDonohue (279)

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

IreoluwaRaufu (200)

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 (592)

@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
(-__- )

IreoluwaRaufu (200)

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

LizFoster (592)

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

IreoluwaRaufu (200)

@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 (592)

@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.

IreoluwaRaufu (200)

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

LizFoster (592)

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

sugarfi (469)

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

LizFoster (592)

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

sugarfi (469)

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

LizFoster (592)

@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 (509)

woah, this is really cool! Nice job!

AdriaDonohue (53)

@LizFoster its really cool!👍👍👍👍

AdriaDonohue (53)

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

AdriaDonohue (53)

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

LizFoster (592)

@AdriaDonohue Oh, cool!

Also, good day ^ ^

AdriaDonohue (53)

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

LizFoster (592)

@AdriaDonohue I love art, but I'm an awful artist myself..

AdriaDonohue (53)

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

LizFoster (592)

@AdriaDonohue Lol

What's the purpose of it? ^ ^

AdriaDonohue (53)

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

LizFoster (592)

@AdriaDonohue Ohhh, ha ha ha! What would I write about in your newspaper?

AdriaDonohue (53)

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

AdriaDonohue (53)

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

LiamDonohue (279)

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

AdriaDonohue (53)

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

AdriaDonohue (53)

@LiamDonohue 😈😈😈😈😈😈😈😈😈

LizFoster (592)

@AdriaDonohue about the warriors game, send me a link to it/the post, id be happy to check it out ^ ^

LizFoster (592)

@AdriaDonohue @LiamDonohue My notifications are exploding... T~T

LiamDonohue (279)

lol i'll stop blowing them up @LizFoster

LiamDonohue (279)

lol its fine im going to bed anyways @LizFoster

LiamDonohue (279)

ohhh no something bad is about to happen... @AdriaDonohue @LizFoster

LizFoster (592)

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

AdriaDonohue (53)

@LizFoster iktaytn(i knew that already your too nice)xD

LiamDonohue (279)

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

LizFoster (592)

@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.

AdriaDonohue (53)

@LizFoster pats on back with symthiny.

AdriaDonohue (53)

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

LizFoster (592)

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

LizFoster (592)

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

AdriaDonohue (53)

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

LiamDonohue (279)

well i can attempt python lol @LizFoster

AdriaDonohue (53)

@LiamDonohue oh'ssss in that case... me neither.