Coding Challenge #140: Pi Approximation with Leibniz Series

preview_player
Показать описание


References:

Videos:

Related Coding Challenges:

Timestamps:
0:00 Introduction
1:22 The Leibniz method for approximating pi!
1:57 Let's start coding!
4:59 Visualizing the convergence
8:45 Drawing a line where pi originally exists
9:43 Debugging!
10:49 Refactoring the code a bit
11:43 Try other series!

Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound

#leibnizformula #piapproximation #p5js #javascript
Рекомендации по теме
Комментарии
Автор

I love how you take the best from my favorite youtubers(SamrterEveryDay, Numberphile and 3Blue1Brown) videos and code it!

jespersvensson
Автор

the series can actually be interpreted as 4*pow(-1, k)/2k+1 from k = 0 to inf

AlgyCuber
Автор

I think you would like the Gauss series for pi, it converges very fast.

idavid
Автор

I love the animation, and the idea of dynamically scaling it while showing a window of recent history.

DaveBriccetti
Автор

This is why I love programming! Doing crazy stuff and getting great and amazing results!

yashasvprajapati
Автор

Literally learned about the Leibniz series last week glad to see an application.

andrewmasek
Автор

I love your Pi day specials. Pi is such a mysterious and interisting number

bapolino
Автор

Hey, just so you know, any time you want to alternate between positive and negative terms you can just multiple everything by (-1)^n. When n is odd it will be negative and when n is even it will be positive, so you don't need that if-else statement.

KakoriGames
Автор

3:50 instead of the conditions modify den --> den = (i * 2 + 3) * (-1)^(i+1). This way you multiply by -1 for even i and 1 for odd 1

Dhukino
Автор

Thanks! Just for giggles, I tried programming this in my SwissMicros DM42 which has quad precision (approx 34 decimal digits). I stopped it when the denominator went past 500, 000 and got to 3.1415_88_. Yes, this series takes quite a while to get reasonably close to π. Going to 2, 000, 003 only gets one more decimal digit. I just bought an Arduino Giga and this would be a good test project.

davethedaemon
Автор

@the coding train... you could add den and then multiply den with -1 ...this makes den negativ every 2 loops because -def *(-1) =def and next loop den*(-1)=-den

idk.
Автор

This (the alternating odd harmonic series) is probably the worst for actually calculating π.
Each new digit requires 10 times as many terms as the previous digit!!
But it's probably the best one for your purpose here, in which you graph how it's doing as it goes.

Because any method that's halfway decent for calculation, would make for a very boring graph.
It would slam down right on top of the actual value (to the precision of the graph) in just a few terms.

But for one of those, you could graph the log of the absolute difference,
yᵢ = log₁₀ |∑₁ⁿxᵢ – π|
which is a bit more abstract, but would show the progress of convergence pretty well.

As for the Ramanujan and Chudnovsky series, you'll need a multiple precision math library if you want to tackle those; each new term adds 8 or 10 more digits!!
These are the ones used by the guys who try to set new records for the number of digits of π. I think they're up to over 10 trillion digits by now!!!

There's the John Machin formula from 1706,
¼π = 4tan⁻¹(⅕) – tan⁻¹(1/239)
using the Taylor series for arctan, but it's complicated to keep track of the number of terms of each of the two, to keep them in balanced precision.

A better choice, that's a good compromise, would be to use the Taylor series for arcsin, to expand:
π = 6sin⁻¹(½)

Thanks for doing these videos – they're truly wonderful and inspiring!
I love the way you can just keep coming up with a flow of ideas, then implement them and see the results right away!

Fred

ffggddss
Автор

Could you make an Color-picker coding challange like wich Shows you the rgb value id love to see that. Loved the Video btw keep up the great work :)

bastianroling
Автор

coding challange 140! sooo close to 141 as in first 3 digets of pi. but still amazing video

luczeiler
Автор

cant wait to see pi-year episodes in 3141

Minty
Автор

Dan, you’re in my favourite youtubers list. It is *REALLY* hard to get in my favourite youtubers list.

ruler
Автор

omg I just coded this when I saw you uploaded this video!

ilayws
Автор

I clicked on the video and paused it, then wrote the Leibniz approximation for pi in 2 lines of Python code:

upper_limit =
pi = sum([8 / (16 * m * m + 16 * m + 3) for m in range(upper_limit + 1)])

lionpersia
Автор

For Leibniz technically you dont start negative. if you consider -1/3 being -1 * 1/3 the first term 1 is a positive 1/1 . Therefore you can simplify a lot of this by doing a for loop that will start you numerator at 1, multiply your numerator by -1 every loop and increment the denominator by 2 and always adding. so (1 * 1/1 = 1) + ( -1 * 1/3 = -1/3) + ( 1 * 1/5 = 1/5 ) which woudl translate to 1 - 1/3 + 1/5.

looking at it this way will eliminate the need for if statement and multiplication of the denominator.

oblivionronin
Автор

Hey I like your videos!
Can you program a rubiks solver plz?

hugolindberg