Learn to Code via Tutorials on Repl.it!

← Back to all posts

Today I'd like to teach you all about one of my all time favorite constants: π!
Most likely, all of you know that π is the ratio of a circle's circumference to its diameter, and probably only know only 3 to 5 decimal digits of it. However, π is an irrational number, meaning it has no end! Let us go through all of the different formulae of π I have programmed, and explain what they mean! Let's dive in. (These are numbered in the order that they print when the attached code is run)

1. The Monte Carlo Method

One way you can do this is by drawing a square, with a circle whose edges touch the 4 edges of that square within. Now imagine you were to draw many many dots randomly within the square-circle hybrid. If you take the number of points that fell within the circle, divide it by the total number of points, and multiply it by 4, you get (you guessed it) π!! More info can be found here:
https://ja.wikipedia.org/wiki/モンテカルロ法

1. The Chudnovsky Algorithm

This is one of the fastest methods out there, being used in the world record, to calculate 50 trillion digits of π! I already explained it in a previous post, but the formula (and more cool information!) is here:
https://ja.wikipedia.org/wiki/モンテカルロ法

1. The Basel Problem

This problem, posed by a man named Pietro Mengoli, asked for the exact sum of an infinite series, with proof. Mathematician Leonhard Euler answered this, with proof, finding it to equal exactly π^2/6. Further info and interesting facts here:
https://ja.wikipedia.org/wiki/バーゼル問題

1. The Wallis Product

Unlike the other formulae here, this one uses the Product Operator in its equation. If repeated over an infinite number of times, it will equal π/2. More information here:
https://ja.wikipedia.org/wiki/ウォリス積

1. The Leibniz Formula

Last but certainly not least, I have here probably one of the slowest π-convergent methods out there. In fact, to get π accurately to 10 decimal places takes about 5 billion iterations, according to the Wikipedia page! This formula alternates between adding and subtracting fractions with odd denominators (meaning this is an example of an alternating series), and converges on π/4. For extra information, go here:
https://ja.wikipedia.org/wiki/ライプニッツの公式

I plan to make more π approximation programs in the future, so stay tuned if these kinds of things interest you as much as they interest me!

Thank you. ^ ^

Great work Liz! Very, very interesting post about a very, very interesting number! I just gave you the conzent creator role, which marks creators who upload high quality content! Congratz

LizFoster (644)

@enigma_dev Oh, thank you! I really appreciate that, that is so awesome of you! Yeah, numbers like π really get me going in terms of conversation, they are just so beautiful!!

LizFoster (644)

@Andi_Chin Why are they a Simp? ; - ;

nice job! also,congrats on geting on the leader bord!

@LizFoster :) do you watch star wars?>:)

@LizFoster i have a starwars oc:

``                                                Chapter1:An Introduction.``

My name is Ariana Bridger.And I am the Most PowerFul Jedi in the universe.That might seem awesome. But it's really not. Because,when you're as Powerful as i am, The Sith find you easily as pie. I’m shocked my crew stayed with me.But they do.That i am glad. Because without my crew I would be dead by now.

When my master made me a full Jedi at 8 years old, everyone was shocked.That was 4 years ago. When I still used my real name. But now, everyone knows me as Adrianna luv.
I was born in an imperial prison to Ephraim and Mira Bridger. When They had to give me to the Sith, they whispered in my ear. They told me to be strong and stand up for what is right. I always remembered that. Their death is the reason for me leaving the empire, and becoming a Jedi instead of a Sith. I never thought i’d see any of my family again, But that was before I joined the Rebellion, and met Ezra Bridger. Now, to tell my story. It started when I got caught stealing a kyber crystal from the empire.
-Ariana Bridger

AphixDev (204)

Sweet! I've been seeing your posts on these pi methods. This is awesome :)

LizFoster (644)

@nt998302 Thank you! I love doing this, it has become quite an enjoyable thing to work on in my free-time (coding and math, that is)

JosiahKnisely (29)

Wait, there are different numbers for π? This is when the confusion sets in...

JosiahKnisely (29)

I'm also very young. I don't get these things.

LizFoster (644)

@JosiahKnisely Oh, that's alright. I can explain. ^ ^

No, there are not multiple values for π. There is only one, that being '3.14159...' and so on. These are merely methods of calculating π, albeit with varying amounts of accuracy at certain numbers of iterations.

π is quite interesting and cool, in that it can show up within the solutions to various mathematical problems that appear to have absolutely nothing to do with circles (at least at first glance)!

The things that the program displays are not π, in that, based on the accuracy I've asked for, the digits it shows aren't EXACTLY π.

For example, a common value that we use when approximating π is 22/7. This is only accurate to 3 digits (3.14), and even closer is 355/113, which is accurate to 7 digits (3.141592).

While they aren't equal to π, we often use them, since they are close enough to be somewhat useful for our purposes!

We don't know what π equals exactly, since it is infinitely long, but these equations can hep us learn more and more.

https://simple.wikipedia.org/wiki/Pi

LiamDonohue (295)

wow you beat me in only like number 5 on hot now lol

LiamDonohue (295)

tho im confused why is this in Japanese lol @LizFoster

LiamDonohue (295)

time to whip out google translate lol @LizFoster

LizFoster (644)

@LiamDonohue You mean for the Wikipedia links?

You know there are English versions... ^ ^*

LizFoster (644)

@LiamDonohue Oh, they're just the names of the methods, and the iterations the code goes through for each one. Nothing special.

Highwayman (1439)

Do you know anything about something called a spigot method/algorithm or something? I heard of them at some point, but I’m not entirely sure where or exactly how either...

LizFoster (644)

@Highwayman Oh! I completely forgot about those, but yes, I do know about Spigot Algorithms! They can be used to find mathematical constants, like π! This is an article I had bookmarked about that:
http://stanleyrabinowitz.com/bibliography/spigot.pdf

LizFoster (644)

@Highwayman Ha ha ha, yeah, it is nothing. (Lol)

I am a bit of a nerd when it comes to math-related subjects (as you can probably tell), so I have many articles like this waiting for use.. wwwwww

Highwayman (1439)

@LizFoster ah yeah that’s kinda me with programming, I have too many books rn XS

LizFoster (644)

@Highwayman Hehehe, same here, I have many books on math I've been saving for a rainy day..
( . __ . * )

Unfortunately I don't have any books on Python or really programming in general (Lol)

Highwayman (1439)

@LizFoster hmmmmmm I’d suggest one of my books but I’m pretty sure non of them are using python :(

LizFoster (644)

@Highwayman Awww, that's too bad.. (Lol)

Highwayman (1439)

@LizFoster ... I saw you were talking about learning "one of the C languages" earlier? That’s what they are all in. I know it’s not my place or anything at all, but I really strongly suggest you do, it’s so awesome.

I’d suggest maybe checking out cryptography or maybe AI Books, those are pretty math heavy I think :P

LizFoster (644)

@Highwayman Ooh, you've got me excited now! I certainly will, then! Have you gotten any specific suggestions for books I should buy?

Highwayman (1439)

@LizFoster ehhh No. :( again all my books are c++, but I bet if you just look it up you will find something pretty easily. At least from what I can remember it’s pretty easy to find(it was just too mathy for me, so I never saved it). There are also some things on ai in this learn section too actually, though of course that’ll be more starter stuff than any of the more exciting stuff.

Actually if you learn C++, I found this one a while back on Neural Networks:
http://www.ece.ubc.ca/~msucu/documents/programming/C++%20neural%20networks%20and%20fuzzy%20logic.pdf

I don’t know if it’s actually good I forget I haven’t picked it up in a while(again- too mathy for me I’m bad at math lol)

And then this one is on analyzing stream ciphers(which are really cool by the way)
http://www.cs.ru.nl/~rverdult/Introduction_to_Cryptanalysis-Attacking_Stream_Ciphers.pdf

Again these are kinda introductions and also just things I pulled from my library, so I don’t how good they are but I hope they are helpful. :P

LizFoster (644)

@Highwayman Oh, nice! These look interesting, I'll start reading these a bit later. Thank you so much! (^ ^ * )

LizFoster (644)

@Highwayman These actually seem pretty up my alley, ha ha ha

Highwayman (1439)

@LizFoster awesome! Perfect I’m so happy they actually are helping someone lol. I haven’t picked them up in a while

LizFoster (644)

@Highwayman Yeah! Math is really fun if you can find some thing or things that you find fascinating (for me it was π, Σ, and complex numbers), and from there, you can find that things that seemed really difficult or complex, just needed a bit more context/clarification.

Highwayman (1439)

@LizFoster ehh idk.... I really like cryptography for example but I don’t even know where to start with the math. I just hit a series of strange unintelligible symbols and math jargon and then have no idea what to do.

LizFoster (644)

@Highwayman Oh, cryptography looks fun. I'd suggest looking on YouTube; half of the stuff I've learned came from Numberphile, 3Blue1Brown, and Mathologer, so I'm sure that there are loads of tutorials and such for cryptography! Here are a few I found if you have the time for it:

and then for fun (probably not as related Lol):

Highwayman (1439)

@LizFoster thank you! I definitely will cryptography is super fun lol :P I’m pretty sure I heard somewhere that @enigma_dev is a cryptography nerd too I think...?

LizFoster (644)

@Highwayman Oh, it says it in his account biography, so maybe he is! (Lol) Glad I could help ^ ^*

Highwayman (1439)

@LizFoster yeah there we go I knew it lol XD

Highwayman (1439)

@LizFoster

If P and Q are two points on an elliptic curve such that kP = Q, where k is a scalar and sufficiently large, it is computationally infeasible to obtain k if k is the discrete logarithm of Q to the base P . Note that this is computationally infeasible even if P and Q are known.

XS ohh boy. Here we go again lol.

LizFoster (644)

@Highwayman Hahahaha don't worry about things that word it like that (I never do). See if you can find a simpler explanation elsewhere

Highwayman (1439)

@LizFoster ok. :) I was actually just going to try and look it up, but that actually sounds like a better idea lol

LizFoster (644)

@Highwayman Yeah, whenever I don't understand something, I look up an article or video on that topic. It really helps!

I was summoned! Yeah, I’m a big cryptography fan! feel free to pimg me with q‘s! @LizFoster @Highwayman

Highwayman (1439)

@enigma_dev Oh, yay! Hi! Um, so
I was reading some stuff on the diffie-hellman key exchange, and they started discussing ECC, which I though was basically just another asymmetric encryption algorithm like RSA, but now I’m kinda confused because it started to look like it was just another form of the aforementioned diffie-hellman exchange, so I guess what my question is is what is elliptic curve cryptography?

LiamDonohue (295)

the only cryptography i know is the Caesar shift wheel lol @LizFoster

Highwayman (1439)

@LiamDonohue hmmm I thought I just saw you talking about SHA256 a little bit ago..? Or maybe that was someone else? Gah I’m going insane I can’t remember anything anymore :(

LiamDonohue (295)

LizFoster (644)

@Highwayman dang, turned down T~T huh

LizFoster (644)

@Highwayman Don't worry, I'm sure he didn't mean anything by it! (Lol)

Highwayman (1439)

@LizFoster yeah, I’d think that. I sometimes accidentally have ppl slip through the cracks so I don’t really blame him.

oignons (313)

@Highwayman Ill let u off with a warning, don't swear.

Highwayman (1439)

@ipastrano oh yeah I forgot about that...

oignons (313)

@LizFoster It is against repl talk rules to swear, so...

Highwayman (1439)

@ipastrano ye I’m deleting it now....

LizFoster (644)

@Highwayman Aaaah my notifications O O

Highwayman (1439)

@LizFoster sorry lol I think we’re Done XD

oignons (313)

@Highwayman thx, i used mat's comment searcher to find it

@LizFoster my brother has like 15 xD

AmazingMech2418 (984)

@Highwayman I'm pretty sure elliptic curve cryptography is just another method of asymmetric encryption which uses the same overall ideas as the Diffie-Hellman Key Exchange while not using the same methods of modular exponentiation. I've never really researched ECC before, but I know it is just another method of asymmetric encryption like RSA and Diffie-Hellman Key Exchange. What I mean by the overall ideas is using another person's public key and your private key to create the same secret as if you used the other person's private key and your public key.

@LizFoster , I would definitely recommend cryptography. It is a great way to delve into the application of mathematics and computer science. For example, with the Diffie-Hellman Key Exchange you use modular exponentiation which is pure mathematics and apply computer science to complete the algorithm and create the shared secret key from the Diffie-Hellman Key Exchange. Of course, however, there are less math-based cryptographic algorithms as well such as monoalphabetic substitution which really isn't even an exact science and just making educated guesses until you get the original plaintext from the ciphertext, but there are also many math-based ciphers and methods such as Diffie-Hellman, RSA, and even, although with much less complex math, Affine Ciphers including the Caesar Cipher which just has an "a value" of 1. The interesting thing about cryptography is that it combines linguistics, computer science, and mathematics into one field of research that can be applied to the real world easily with the encryption and decryption used on the Internet (usually RSA, but other ciphers are used occasionally).

If anyone has any questions about cryptography, feel free to ask me. I have been studying cryptography on the side for a while now, even to where I ended up getting first place in a cryptography competition earlier this year.

Highwayman (1439)

@AmazingMech2418 ye thay’s what I’ve been thinking but I haven’t been so sure. Thanks!

LizFoster (644)

@AmazingMech2418 Wow, good work! I'll definitely look into it when I have time, then! #^__ ^#

AmazingMech2418 (984)

@Highwayman You're welcome! By the way, I recently created a program that uses the Diffie-Hellman Key Exchange for end-to-end encryption with private keys generated from hashes of passwords which are encrypted to create a number, but I currently am using a modified ASCII and Vigenere hybrid to use the shared secret for E2EE. Do you have any suggestions on how to use the shared secret differently for increased security since the Vigenere Cipher can easily be broken with a "crib" (plaintext attack) or a kappa test? I can't use a hash since I need to decode it as well. It needs to be symmetric as well so that it can use one shared key. I know I could add on an extra layer of a transposition cipher or something, but am waiting to do that for right now, but if you have any ideas that are much more secure, I'll at least write them down and save them for a later update.

Highwayman (1439)

That sounds sick btw XD

LizFoster (644)

@AmazingMech2418 Hm.. Can you send me the link to the code? I want to see what I'm dealing with. (Lol)

I know you weren't asking me, but I'm interested!

AmazingMech2418 (984)

@LizFoster Thank you! Also, I could explain some of the ciphers I mentioned if you would like or you could just look them up or something. Affine ciphers are definitely the best place to start, then likely monoalphabetic and Vigenere and on from there to more advanced ciphers like RSA.

LizFoster (644)

@AmazingMech2418 I like that the "best place to start" ciphers have ridiculous and confusing names, and then the "advanced" one is "RSA" wwwwwwwww

Sure!

Highwayman (1439)

@LizFoster I mean technically RSA is worse: it)s the combination of three different names mashed together XD

AmazingMech2418 (984)

@LizFoster The code is here: https://repl.it/@AmazingMech2418/HoustonEverywhere It is in html/home.html in the long JavaScript code section. I think I mention E2EE in the comments somewhere...

LizFoster (644)

@AmazingMech2418 Aww, darn it. I really wish I knew JS, then I would know what was happening!! Curses! (Lol)

AmazingMech2418 (984)

@LizFoster So, affine ciphers just follow the expression `ax+b mod 26` where `mod` is the modulus function and `x` is the alphanumeric code from the letter you are given. The alphanumeric code is where a=0, b=1, c=2, ... Monoalphabetic substitution is where every letter stands for another distinct letter (no two letters stand for the same letter) without any equation where you just have to guess and check. Vigenere is where you have a key that repeats along the length of the plaintext (text you are encrypting) or ciphertext (text you are decrypting) and you add the alphanumeric value of a letter in the plaintext with the corresponding letter of the key or subtract the alphanumeric value of the key letter from a the letter in the ciphertext. Then, RSA uses `v^e mod n` where (e, n) is the public key and v is the value to encrypt. You also just change e to d (private key) to decrypt a value as well. Those are just the basics of some of the ciphers. Would you like me to explain further about how to do plaintext attacks and how to decode monoalphabetic ciphers?

AmazingMech2418 (984)

@LizFoster https://w3schools.com is a great place to learn JavaScript. At least, that's where I learned it. Also, of course StackOverflow helps for the more complex stuff.

LizFoster (644)

@AmazingMech2418 This is... complicated. Reminds me of when I learned about complex numbers. What an ordeal.. T~T

It is cool, though! I'll definitely check it out, then.

AmazingMech2418 (984)

@LizFoster Are you talking about the cryptography or JavaScript? Really, with cryptography, you just need to know what cipher does what and be able to decrypt and encrypt based on the cipher. Also, eventually, you'll need to be able to identify the cipher based on what it looks like and the situation, but that's the more complex part of cryptography which is also not really an exact science (trust me, there are many things I thought were base64 that weren't).

LizFoster (644)

@AmazingMech2418 Yeah, the cryptography. You make it sound so easy.. (Lol)

Again though, it really does sound cool. I'll try to read up on it.

AmazingMech2418 (984)

@LizFoster With me making it sound so easy, though, remember that I've been doing this for years (however, only two years in-depth, although I knew the Caesar Cipher and Pigpen Cipher (special case monoalphabetic substitution cipher that is EASY (look it up on Google Images and if you see a tic-tac-toe board-like thing with an "X" at the bottom, that's the entire cipher and how it works)) long before). Also, with the cryptography, I have realized that it is easier to learn by doing than by reading. This website can auto-generate keys for ciphers given a quote and has practice tests: https://toebes.com/codebusters/ . Don't worry about where it says "Div A", "Div B", and "Div C", that is just the different "divisions" in a competition, labelling which divisions use which ciphers. I actually ended up learning ciphers like Vigenere and the Affine ciphers through that website by just finding patterns in generated codes.

Highwayman (1439)

@AmazingMech2418 since ya’ll are blowing up my notifications so much I might as well let you know that I think I just finished a body parser.

AmazingMech2418 (984)

@Highwayman I just saw it and that is basically what I have already. The issue comes in when you put two sets of brackets or more.

Highwayman (1439)

@AmazingMech2418 two sets of brackets? Could you provide an example plz.

AmazingMech2418 (984)

@Highwayman like a[b]=something or with arrays a[]=something or a[b]=something or a[]=something

AmazingMech2418 (984)

@Highwayman I don't know. I think we might have overflowed the Repl.it servers with this conversation on cryptography... I submitted a bug report already. Could you upvote it so it gets fixed sooner?

Highwayman (1439)

@AmazingMech2418 I can’t, the bugs page and the feedback page are blocked on my iPad :(

Highwayman (1439)

@AmazingMech2418 Also, I think my program can handle the example you gave I think. I updated it since I gave you the link.

AmazingMech2418 (984)

@LizFoster Even if you click "View All"?

AmazingMech2418 (984)

@Highwayman Also, your repl doesn't seem to work with something like `a[b][c]`.

Highwayman (1439)

@AmazingMech2418 really? Worked for the example I tested it on hm that’s strange... wait do you mean with no value or something?

LizFoster (644)

@AmazingMech2418 Oh. That's odd, it doesn't load that way. Sorry.

Highwayman (1439)

@AmazingMech2418 ah yes see what’s happening with my prog ok let’s see....

Highwayman (1439)

@AmazingMech2418 no wait I don’t. It seems to work perfectly for that.

AmazingMech2418 (984)

@LizFoster There is no reason to be sorry. It's a bug with Repl.it. I already submitted a bug report, so hopefully it gets fixed soon.

AmazingMech2418 (984)

@LizFoster My bug report is here: https://repl.it/bugs/p/lets-talk-about-talk-post-returning-404-error The issue is still going on. I'm not sure exactly what brings bug reports to the top of the list, but I think it is voting on the bug report. In that case, can you upvote the bug report so that this gets fixed faster? Also, probably commenting would help as well, like maybe saying that it was happening for you too.

LizFoster (644)

@AmazingMech2418 Ah, okay, I can do that ^ ^ Thank you so much!

AmazingMech2418 (984)

@LizFoster Thank you and you're welcome! However, I just noticed that the same thing happens on the comments tab of my profile. https://repl.it/@AmazingMech2418?tab=comments . However, for some reason, it is not happening on yours or @Highwayman 's. Also, for the cryptography thing, I'm probably going to create a learn post about it.

LizFoster (644)

@AmazingMech2418 That's quite odd..

Nice, tag me in it when you post it!

AmazingMech2418 (984)

@LizFoster I posted it and tagged you! However, it's still a work-in-progress.

AmazingMech2418 (984)

@LizFoster The bug report was just forwarded to the Repl.it engineers and should be fixed soon!

Highwayman (1439)

@LizFoster oof XD why are they all their own functions?

AmazingMech2418 (984)

@LizFoster It would probably be easier if you could import an image into Python... Then, at least, you wouldn't have to program the graphics for EVERY character.

Highwayman (1439)

@AmazingMech2418 oh? O.o OH WAIT THAT’S WHAT THAT IS

LizFoster (644)

@AmazingMech2418 @Highwayman The problem with relying on images is, you can't easily manipulate them for the turtle's purposes. I'd also just prefer to be able to make the maps from scratch! (Lol)

AmazingMech2418 (984)

@Highwayman Yep! I actually found the repl before the screenshot though.

Highwayman (1439)

@LizFoster why not instead of making a bunch of individual functions with the images hard coded, make a generic function like
draw(state,sprite_map): ...
and have maps for each guy. I mean textures. Images. What ever you call it. Textures. Lol

LizFoster (644)

@Highwayman Well, that's kind of what this would do, but classes make it easier to store all of those sprite maps.

Textures, maps, Images, call it what you'd like wwwwwww

Highwayman (1439)

@LizFoster ... I mean I’m confused on how that works but ok lol how is the classes making it easier? What are that list of functions with state passed going to do then that is different from each other?

LizFoster (644)

@Highwayman Because all of those sprites have two different states, to simulate movement:

Highwayman (1439)

@LizFoster I meant how is the code within each of those functions going to differ besides what image they draw sry lol should have clarified :P

LizFoster (644)

@Highwayman Oh, ha ha. It won't differ too much, but using a class to store the sprite maps allows me to easily consolidate and collapse all the lines I don't need to see at any given point. In other words, it just lets me access what I need to much quicker!

Highwayman (1439)

@LizFoster ahhh wait I get now ok yeah :)

Highwayman (1439)

@LizFoster O.o oh no.... I’m getting the error again! O O

Highwayman (1439)

@LizFoster lol I think we just talk so much we broke the post XD

Highwayman (1439)

@LizFoster :P I’m starting to think definitely lol I looked at the comment count on this post: it’s nearly 150!! I mean granted I’ve had that many comments before but still...

LizFoster (644)

@Highwayman My matrix system works! Hooray!!

Highwayman (1439)

# YAY!

:D

LizFoster (644)

@Highwayman I just finished another model; this may be a bit easier than expected! #^__ ^#

LizFoster (644)

@Highwayman Yeah!! It is stupid, I know, but I am already proud of how it is shaping up to be!

Highwayman (1439)

@LizFoster nahh it’s definitely not stupid, how could it be lol your making graphics Besides I’ve heard it said many times: the coding is the easiest part. You had to plan all the other stuff first lol.

Highwayman (1439)

@LizFoster oo! I have 1111 cycles! :P

LizFoster (644)

@Highwayman Yeah, you're right.

Also, good work! (Lol)

Highwayman (1439)

@LizFoster OH YEAHHH! I’m so dumb XD yeah thank you :P