And Ramanujan had a series that converges blazingly fast to π. Refinements of it are even a bit faster, and are used even now to compute those zillions-of-digits-of-π tables. What is the Gauss series for π? Fred
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
@@TheCodingTrain Is my math training showing? ;-) Anyway, this is a wonderful playground, and a learning tool, all rolled into one - it's exactly what I wish could have been available 50 years ago, when I was learning computer languages! Fred
@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
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.
//For loop without if checks: double quaterPi = 0; for (long i = 1L; i < 123456789L; i += 4L) { quaterPi += 1.0d / i - 1.0d / (i + 2L); } double Pi = quaterPi * 4.0d;
I clicked on the video and paused it, then wrote the Leibniz approximation for pi in 2 lines of Python code: upper_limit = 1000000000 pi = sum([8 / (16 * m * m + 16 * m + 3) for m in range(upper_limit + 1)])
lionpersia I’m pretty familiar with basic Python, but how does that work? I don’t see anywhere that would alternate sign, and I think you messed up the closure, because there’s no ].
@@KnakuanaRka The alternating term (-1)^k / (2k+1) from k=0 to an upper_limit under the summation sign is both verbose and very slow. So, with a very simple acceleration trick, namely, summing the two consecutive terms into one (since k = 2m and then 2m+1), you get (-1)^2m / (4m+1) + (-1)^(2m+1) / (4m + 3) = (-1)^2m * [1/(4m+1) - 1/(4m+3)] = 2 * (-1)^2m / (16m^2 + 16m + 3) = 2 / (16m^2 + 16m + 3) through m=0 to an upper_limit. The leading 8 in my formula in my original comments comes from accounting the conversion of pi/4 to pi. The closing ] was omitted mistakenly, but I edited it in.
I have a suggestion for a future coding challenge (I don’t know if you accept suggestions from youtube comments or at all, but if you do and this isn’t the right place for a suggestion then let me know where to put it): I found this thing called a swinging Atwood’s machine (en.m.wikipedia.org/wiki/Swinging_Atwood's_machine) and I’ve tried to make a simulation of it but I kinda bit off more than I could chew and just gave up out of confusion. Could you make one in a future coding challenge?
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.
This guy is sitting here wondering how to cure cancer, while i am here being annoyed because in two minutes, the history list would be 7200 items long, which means that in two minutes, the computer would have to sift through 7200 items every 1/60th of a second. I recommend you to remove the history list altogether. There is another way to make it act similar to that without the list.
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.
i believe that we can improve the rate of convergence of this method if we accept the pi(n) to be pi(n) = (Leibniz(n) + Leibniz(n-1)) / 2, where Leibniz(0) := 0, instead of Leibniz(n).
I was trying to build an Apollonian Gasket for my professor a while back but I ran into a problem with my method of circle inheritance. Pi, along with many other interesting patterns, is present in the Apollonian Gasket and it’d be really cool to extract that and other sequences from it.
I usually teach while loop in Python with this formula... Have a look to the Plouffe formula or Machina formula. One interesting thing to do is to use the recurrent version of such processes.
CHALLENGE: MUNSELL COLOR SPACE. Every block is a different color because of the equation and so are all their positions in the 3d space. I have done this for my Math and Computer Graphics class and it looked very beautiful and was worth the Final Project for a student like me at the time. This was a group project and we all completed our assigned roles. I don't remember much but it was very interesting researching about how it worked. I'd love to see you complete this.
You're probably thinking of the Machin-like formula! Combining the Taylor series for arctan with Machin's formula one can approximate Pi fairly easily! en.wikipedia.org/wiki/Machin-like_formula
John Rachidy That’s a joke, of course you are correct and besides, the sum of all natural numbers is divergent series and partial sums make no sense in getting closer to -1/12, because this limit cannot be calculated that way :)
This is what I've been using with Java. Let me try to make a Processing example with it and add soon! docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
There is one and only operator with three inputs: the conditional operator. Calling it the ternary operator is like calling an octopus the octopod creature. Yes, I know. Everyone does it.
Why didn't you just multiply the fraction by -1 to the power of i or i+1 to have it alternate between even and odd lol, then you could have avoided that if statement and not use modulus XD.
U can avoid modulus with a power of 2 by bitwise & (AND). e.g. N%8 == N&7 All muliples of 8, in binary representation end with 3 zeros (8=2^3), so by AND-ing with 7 (binary ..0111) u keep only the last 3 digits which are the modulus of that number. In general N%m = N&(m-1) when m is a power of two. Why would u want to do this? Division is slower than bitwise AND, however compilers already know those tricks and u wont get an extra benefit. Now to avoid that if-else branch, based on modulo operation, u can of course use pow(-1,k). But this requires an extra function call (assuming for this case, that the compiler doesnt optimize it). To avoid this, by looking at the code we see that when k is even or odd we want negative or positive sign respectively. k%2==0 ? We want -1 k%2==1 ? We want +1 By looking at the graph of the function we want, its easy to see its the line y=2*x-1, so instead of pow(-1,k) we can write 2*(k%2)-1.
@@TheCodingTrain OMG YOU REPLIED TO MY COMMENT! DUDE, I JUST WANNA SAY I FREAKING LOVE YOUR VIDEOS, YOUR VIDEOS ON FIREBASE SAVED MY ASS AT A HACKATHON AND MY TEAM WON FIRST PLACE! THANK YOU SO DAMN MUCH!