Share your repls and programming experiences

← Back to all posts
EVEN MORE APPROXIMATIONS OF π!!
LizFoster (568)

I'm back at it again, this time with a compilation of different formulae for approximating π, with surprising accuracy in some cases!

Fascinating stuff!

Commentshotnewtop
CodingCactus (2121)

Great and interesting as always!

LizFoster (568)

@CodingCactus Thank you! I try my best (Lol) ^ ^*

luffy223 (6)

I have a way of approximating pi but i do not now if it is plausible:
in calculs we know that an integral = area under curve
and from geomtry area of circle = pi(r)^2
the circle furmula is x^2+y^2=r^2 solve for y :
y=((r^2)-(x^2))^(1/2)
(interrating from -r to r = area of half a circle)
2 = area of circle
then solve for pi :
pi = (area of circle)/(r^2)
to calculate the intergration use riymen's sum or simpson's rule
i know how to solve simpson rule only on desmos
is it plausible

LizFoster (568)

@luffy223 Ah, yes. You can! Like this!

If this is what you meant:

The outcome of the integrated equation is the area under the curve of a semicircle (with r = 1). It is also π/2. Multiply it by two, you get π, which is the area of a circle with (r = 1)!!

LizFoster (568)

@luffy223 Alright, part 2, done!

This uses your typical Riemann Sum formula to calculate the area under the curve. The higher n is the better, but it lags too much to be worth it _ _

The difference between the outcome (a in this case) and π is just about:
0.0000000000032249758419

So yeah, pretty accurate for how low n is ^ ^*

LizFoster (568)

@LizFoster And now, the final addition allows r to equal any integer value (otherwise it returns undefined _ _)

luffy223 (6)

yeah like this

here using simpson's rule


@LizFoster

luffy223 (6)

but could you make a python program to delet the lag from [email protected]

LizFoster (568)

@luffy223 Interesting! I'm quite fond of these two methods, I may actually add them to "π approximations 3" (which will be coming soon) _ _*

luffy223 (6)

cool will wait for it see ya @LizFoster

LizFoster (568)

@luffy223 Sure thing ^ ^

Should I tag you in the post when I do upload it?

LizFoster (568)

@luffy223 Alright, so I am a bit confused as to how I would go about saying:

*f(-r + (a-b/n) x i)*

or

*f(l_i)*

Obviously, I understand how to do the inner workings, but is there something specific I must do to add in the f() part?

Also, funnily enough, if you multiply the second part of the Riemann sum by (τ x 10^3), it does the same thing as f() does. However, since τ is just , I'd hardly count that that as a win.. (Lol)

luffy223 (6)

mmm to be honst i didn't understand your question (hahaha) mybe def F(): @LizFoster

LizFoster (568)

@luffy223 Sorry, ha ha ha!

What I mean is, what is the difference between these two?

(-r + (a-b/n) x i)

f(-r + (a-b/n) x i)
luffy223 (6)

this is how I undertanded your [email protected]

luffy223 (6)

yeah and there are three types of xi :
ri = a+ idelta(x)
li = a+ (i-1)delta(x)
mi = a + (i-0.5)delta(x)
for simpson rule I used to xi typs :
li
and oi = a+ (2i-1)delta(x)@LizFoster

AgastyaSandhuja (144)

ok this is unrelated but that response was FAST @LizFoster

LizFoster (568)

@AgastyaSandhuja Lol (^ ^ )

I was active at that second.

AgastyaSandhuja (144)

BTW did you get the value of φ
from
ΔΔG D-N < 1.7 ?
@LizFoster

LizFoster (568)

@AgastyaSandhuja How interesting. No, I did not use that; I merely calculated it via the formula (√5 + 1) / 2. However, this looks intriguing, and I'll do some reading on that!

AgastyaSandhuja (144)

Yes it's interesting! It is in equa6 in your code, but whatever you say :p @LizFoster

LizFoster (568)

@AgastyaSandhuja What I meant is, I have not heard it in that context ^ ^*

Highwayman (1250)

Lines 5 and 6 are scaring me why do you need to know the amount of days in a week and amount of days in a year?

Edit: oh. Um.

LizFoster (568)

@Highwayman Ha ha ha! For the sixth equation, it uses weeks and year as units (which in my opinion is incredible. Why do units of time produce π?!).

Highwayman (1250)

@LizFoster my first(reasonable) thoughts were about how the earth circles the sun, creating such schedules..?

LizFoster (568)

@Highwayman Ooh, that's a good point! I can see it now..

Highwayman (1250)

@LizFoster :P that would be so cool if there is an actual correlation.

AgastyaSandhuja (144)

Actually, a lot of methods of pi do include the earths orbit and time.

@Highwayman @LizFoster

AgastyaSandhuja (144)

oh wait that's in your program xD

Highwayman (1250)

AgastyaSandhuja like yeet

LizFoster (568)

@Highwayman @AgastyaSandhuja Ha ha ha! I am still interested as to why it is used (and why it works..)!

AgastyaSandhuja (144)

Me too! Again. fast response. (2 seconds) how do you type so fast?
@LizFoster

LizFoster (568)

@AgastyaSandhuja Lol

I'm just active at the moment, and I see the notifications pretty fast.

StudentFires (291)

JS https://repl.it/@StudentFires/pJS,
C++ https://repl.it/@StudentFires/pC
That is the result of a tutorial on memoization, hours of work on converting loops to recursion, converting mathematical summation and product notation to code, then to recursion, and plenty of Desmos graphing and more.
I'll be back on Repl in about 5 hours.

LizFoster (568)

@StudentFires Sorry, had to take care of something ^ ^*

Nice! Looking good

StudentFires (291)

@LizFoster Eh, I wasn't on Repl. Also back when I did this I removed plenty of redundancies from the Riemann Sum formula that you posted earlier.

LizFoster (568)

@StudentFires Oh, huh. I can see what you mean, those would vastly improve the formula aesthetically. Is it any faster?

StudentFires (291)

@LizFoster Never implemented it, but I'm pretty sure r-(-r) is probably slower than 2r.

LizFoster (568)

@StudentFires Hm. I'm not fully sure what is going on there, can you write it out on Desmos?

(In response to your other post about a different algorithm)

StudentFires (291)

@LizFoster That algorithm was wrong, I did it in JavaScript a few minutes ago, so I deleted it.

LizFoster (568)

@StudentFires Ah, okay (Lol)

That would explain why I could not comment on it wwwww

StudentFires (291)

Do you know if this an effective way to calculate π?
I believe I'd need p(n)/n to calculate for π after the function.

p(z) = ProductNotation(n=1, (2n/(2n-1))^2, z);
π = p(∞)/∞

Update:

LizFoster (568)

@StudentFires Yes, actually! That formula is incredibly similar the Wallis Product (the second part of the product has a '+ 1', not a '- 1')!

StudentFires (291)

@LizFoster Well, since it works, I’ve converted it to a recursive function, thus I can memoize it... That or I’ll do it C++.
Also what has a +1?

LizFoster (568)

@StudentFires
(Also I just remembered, the Wallis Product results in π/2, and this results in approximately π when multiplied by some large number, m, which is the number of iterations)

Sweet! I can't wait to see it!

StudentFires (291)

@LizFoster That’s why your example divided by m, and I divided by z. Also, 2π is more important that π itself, but I forgot its name. Also, usually i or k are used as the counters in Product Notation and Summation, so I should fix my examples.

LizFoster (568)

@StudentFires Oh, it is called τ (tau)!

StudentFires (291)

@LizFoster Yeah that... “Tau”. That’s far more important that Pi itself.

LizFoster (568)

@StudentFires Yes, it certainly is. However I haven't done much reading on it as I'm quite fond of π (as you can tell wwwww)..

StudentFires (291)

@LizFoster I assume you don’t know JavaScript? I'm failing miserably right now.

LizFoster (568)

@StudentFires Ah, alas, I do not. I've been considering learning it though..

If you need help you should try your luck on Stack Overflow, or just post on the Ask forum. ^ ^*

StudentFires (291)

@LizFoster The Ask forum here isn't really active, and yesterday I asked two questions on StackOverflow, no one answered either of them, not even a comment.

LizFoster (568)

@StudentFires Oh, that's weird. Normally you get at least one comment... Maybe it was a really active/really inactive moment? Your questions might have drowned in a sea of other questions, but I don't go on there too often, so I wouldn't know.

StudentFires (291)

@LizFoster It was drowned fast, yeah.

LizFoster (568)

@StudentFires Darn, that sucks, I am sorry T~T

StudentFires (291)

@LizFoster Update, actually my code was perfectly formed... but I forgot to put a semicolon. https://repl.it/@StudentFires/PreciousAssuredPi, And what a coincidence, I haven't even renamed the Repl yet.

LizFoster (568)

@StudentFires Ha ha ha! Happens to the best of us (I say from experience, I've gotten hung up on things, only to find that it was failing from a basic syntactical error)..

Looks good! Can you allow it to take a user input for the iterations? It is fairly inaccurate in its current state.

Really fast either way!

StudentFires (291)

@LizFoster Yeah, I updated it to be interactive a minute after I told you.

LizFoster (568)

@StudentFires Sorry for the late response, had to eat breakfast ^ ^*

Ah, nice! It actually takes about as long to calculate
1 000 000 000 as Desmos does, so it's REALLY fast. Wow.

StudentFires (291)

@LizFoster Don't worry, I wasn't even on Repl.it for a few hours, so I'm sorry for the late response. I updated it to C++ and never finished it, it has plenty of room to be faster and more precise, I'm getting on it right now!

Wait... breakfast, what continent are on?

LizFoster (568)

@StudentFires Ah, all good. I'm excited to see how fast it is once you finish it (#w#)

Oh, nowhere wwwwww

StudentFires (291)

@LizFoster I'm all the way in the redacted. Is the virus bad where you are?

LizFoster (568)

@StudentFires Not too bad (well, as 'not bad' as it can get with COVID-19) luckily. Is it bad where you are?

StudentFires (291)

@LizFoster Hasn't even touched my city... yet. But it will, unfortunately, as it's very close.

But back to that formula I'm using, I thought about it and 2x/(2x-1) can be simplified to x/(x-.5), dunno why he didn't do this himself.

LizFoster (568)

@StudentFires Wouldn't that just give half the result though?

LizFoster (568)

@StudentFires Oh, shoot, nevermind. I am dumb _ _

You're right, that would work as well.

StudentFires (291)

@LizFoster You aren’t dumb, now if you didn’t catch your own err, maybe.
But I’m pretty sure you’ve been taught a higher level of Mathematics than I’ve been taught, since I’ not even in pre-Calculus yet.

LizFoster (568)

@StudentFires I re-consolidated the equation, so it looks a lot more clean now.

StudentFires (291)

@LizFoster Bit late, I already have that. In to code too.

LizFoster (568)

@StudentFires I suppose you're right.. ^ ^*

Trust me, calculus is a lot of fun; it is just that, most calculus teachers are really boring. You should watch 3Blue1Brown's "Essence Of Calculus" series if you are interested in that stuff. He explains it in an easy to understand way, and it's entertaining too (which cannot be said of most calculus teachers)!

LizFoster (568)

@StudentFires Aaaaaa darn it! I felt cool for a second there T~T

StudentFires (291)

@LizFoster Go look at the very top of this message chain. Carefully look at the first images I posted. I had the squared version in the very beginning.
Always 1 step ahead

codeitfast (11)

Nice! You could also use sin and cos!

LizFoster (568)

@codeitfast Thank you so much!

Yes, I could, but apparently they are based on π themselves.. I don't want to use things derived from π to find π

codeitfast (11)

@LizFoster true. However, if you know calculus, you can also use it to find the area and then, when you have the area, since you have the radius, find pi.

LizFoster (568)

@codeitfast Ooh, good point. As luck would have it, I do know calculus! (Lol)

LizFoster (568)

@codeitfast Now, how exactly would I use sin for that? It has something to do with internal angles (at least, I would assume so), but otherwise I am in the dark..

codeitfast (11)

@LizFoster you actually could just change x by something like .00000001 until you get 180, then use the Pythagorean theorem during each interval to find the circumference, and you could get pi.

LizFoster (568)

@codeitfast Good point, though could you explain that a bit more? what is x an argument to? tan(x)?

[deleted]

Oh wow. It took 10 minutes to calculate the numbers.

LizFoster (568)

@CodeABC123 What do you mean? It takes less than 3 seconds..?

[deleted]

Not for me. @LizFoster

[deleted]

Maybe you have a fast computer. @LizFoster

LizFoster (568)

@CodeABC123 Oh, how weird. That's surprising, as I am on a fairly low-end chromebook (Lol)

[deleted]

I’m on an iPad. @LizFoster

LizFoster (568)

@CodeABC123 Ah, I see, that probably explains it ^ ^*

Sorry it takes so long..

cs906941 (3)

Im not a great programmer, but maybe you could use the leibniz formula? Where you start with an approximation of pi at 1, and then subtract 1/3, add 1/5, subtract 1/7. add 1/9, etc. That times four is pi. I have a repl for this on my profile if you're interested.

LizFoster (568)

@cs906941 Funnily enough, I have that as well! You can find it here: https://repl.it/@LizFoster/140

One of the first approximations I made, actually, ha ha ha!

finlay44111 (39)

for equation 7 you are using sine, which is based on pi itself. You should look into some infinite sums, there are some really simple ones, like 1 + 1/4 + 1/9 + 1/16 + 1/25 + 1/36 (reciprocals of square numbers) and that infinite sum tends to ((pi)^2)/6

LizFoster (568)

@finlay44111 Oh, I see, I did not remember that sin was based on π.

Is that series (the reciprocals of square numbers) by chance related to ζ(2)? I am not sure if they are related, but here is the equation I refer to (from the Wikipedia on ζ):

If not, what is that infinite sum called? It looks to be the same as the Basel Problem, which I think I have done.. (might be wrong though, Lol)

finlay44111 (39)

@LizFoster yeah that is exactly the series it is, it is riemann zeta function, you can actually get pi from any even value you pass into the riemann zeta function, eg, riemann-zeta(4) == (pi^4)/90

finlay44111 (39)

@LizFoster also the Basel problem is the same as the riemann-zeta(2)

LizFoster (568)

@finlay44111 How interesting, I never thought about that!

LizFoster (568)

@finlay44111 Okay, I thought so, but wasn't fully sure (Lol)

StudentFires (291)

@LizFoster
I remembered someone who loved big numbers, so I thought you might like to see this: https://repl.it/talk/share/Using-Memoization-to-Speed-Up-Code/31937.

LizFoster (568)

@StudentFires Oh wow, that's awesome! I'll try to puzzle my way through the code (^ ^**) (Lol)

StudentFires (291)

@LizFoster The only problem is, I'm not sure if Python can return functions, so this might not be possible in Python, but I might be able to find a way to calculate π quickly.

LizFoster (568)

@StudentFires Wait, do you mean printing functions that return a value? Python can do that if so.

StudentFires (291)

@LizFoster Well then, perfect! The idea can be implemented in Python. Sorry, I don't know Python... yet.

LizFoster (568)

@StudentFires That's perfectly fine, don't worry about it!

Ha ha ha.

I'll see what I can do about getting one written for python... Right after I finish writing a code for finding all Pythagorean Triples with side c being 50 or less

I am going to have a long night.... T~T

StudentFires (291)

@LizFoster Unless you're trying to challenge yourself on purpose, I suggest leveraging the internet to reduce your workload. Just maybe your night won't be as long.

LizFoster (568)

@StudentFires Yeah, you're right... ( ̄ヘ ̄); It would make things a lot easier, ha ha ha

I do not even have to do this tonight, I just hate having to take more than a day to finish stuff like this. (Lol)

IreoluwaRaufu (200)

Those Unicode character variable names are astonishing.

IreoluwaRaufu (200)

@LizFoster Yeah, they inspired me to experiment with variable identifiers, and there's actually a wide range of characters you can use to name a variable, list, or other data structure in Python3. (The more ya know.)

LizFoster (568)

@IreoluwaRaufu Yeah! Although, I still need to check if Japanese symbols are valid. Probably not ^ ^*

JadenGarcia (34)

They can be more accurate. Also, more a more precise value of "year," use 365.25, or find a more accurate decimal.

LiamDonohue (273)

how did i know this would happen lol

LiamDonohue (273)

yup next thing i know u will be doing quantum physics lol @LizFoster

LizFoster (568)

@LiamDonohue Don't even get me started... (Lol)

UzayAnil (19)

noooooooooooooooooiiiiiiiiiiiiiiiccccccccccccce!

(noice)

Roar123 (305)

Amazing, I've never seen this many pi approximations in one program in my life!