Calculate Pi on a Raspberry Pi Using the Leibniz Formula for π - Pi Day 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so today is March 14 March is the 3D month this is 3.14 so of course it's Pi Day and as I've done in previous years I want to show a method of how you can calculate a value for pi and this time we going be doing on a Raspberry Pi a Raspberry Pi 5 so if you want to find out more please let me explain so in this video we're going to be calculating Pi on a Raspberry Pi a Raspberry Pi five and of course there are many many different ways in which Pi can be calculated so in 2019 I did a video for Pi Day about the nil canther infinite Series today we're going to be using a different method but if you're interested in that one go back and check out that video okay so this year we're going to be using the leanas formula for calculating pi and it's really really really simple as it says here so pi over 4 so a quarter of Pi is equal to 1 - 1/3 + 15 - 17th + 1 9th and so on and so forth so it's really easy because after you do all these sums you get a value for a quarter of Pi and you multiply it by four and you get the the value for pi also notice the series is really easy because it's a third a fifth a seventh a 9th so that's really easy to calculate and it just flips between subtraction and addition subtraction addition so nothing no complicated formul to understand here some of the formulas for calculating Pi can be a bit dizzying this is really easy to understand and it's one of these uh formulas that the more you calculate the better value of pi you get and we'll talk more about that in a second it's also worth mentioning that this uh sometimes has other names in fact it's been discovered by three different people there was an Indian mathematician who discovered it in the 14th or 15th century and then it was later independently rediscovered by James Gregory in 1671 and then liet in 1673 so it's a sufficiently well-known formula now an infinite series is a set of sums in an infinite sequence that means there is no end to it so it just keeps on going and keeps on going and keeps on going uh not just to a thousand terms or a million terms it's just there is literally no end and infinite series have uh some different characteristics some types of infinite series converge so they they hone in on a certain point and they converge towards a number and this series converges on the number of pi now because Pi itself is infinite there is no end to it and because it series is infinite you're never going to get to the end of Pi but as you process more and more terms do more addition and subtraction according to that formula you're going to get closer and closer to a definition of pi and there are some infinite series that diverge that means they go off towards infinity or they oscillate here and there without settling on any particular value or they behave in other erratic ways but they're Divergent you don't know where they're going to end up so we can see here after a th terms so 3 5 7 9 just keep going for a thousand of them then this series will calculate pi to be 3.1 and then 395 so obviously you know you could round that up to 3.14 but it's not quite there yet so th000 terms gives you something close to 3.14 but not there after 5,000 terms you get 3141 so that's good after 100,000 terms you get 3.1415 so that's good after a million terms you get 3.14159 and then you go to 10 million terms you'll get 3.141592 and then you can keep going more and more terms uh each time get closer in terms of decimal places closer to the value of pi now the disadvantage of this is that it converges very very slowly calculating Pi to 10 correct decimal places uh requires 5 billion terms so lot of work there just to get 10 decimal places and there are ways of speeding up an infinite series like this but to discuss those is beyond uh the scope of this video now one good thing about this infinite series it's really easy to understand it's just just addition and subtraction now addition of course is commutative so 3 + 5 is the same as 5 + 3 so it doesn't matter what order you do them and a subtraction is anti-c commutative in that 5 - 1 is equal to the minus of 1 - 5 so I don't know about you but when I'm doing things on a calculator if I type them in the wrong way and I get minus number I just drop the minus and I know what it should have been if I type the terms in the right around so if you if you're careful with the terms you can actually make subtraction uh commutative you can do them in any order so that means with this we've got uh the ability to do some addition and do some subtraction and actually we can do them in any order that we like as long as we're careful about the terms and the minus sign and that means writing code for this is really easy because it's just this plus this or this minus this fairly simple to write now there is the one over part that we have to do but that's the same for every term so once we do one over something then and that's part of the calculation that's the same for every single term now since the order doesn't matter and if we are careful with the signs we can split this into multiple tasks that means we can write a multi-threaded program so that's what I've done I've wrot a multi-threaded program that calculate pi because each thread can handle some part of that infinite series and then we can bring all the results together at the end to do one final sum to work out what the value should be in this example I'll just split it into two tasks one that does all the additions and one that does all the subtractions now that's really really easy all the additions all the subtractions and you bring them together at the end to give you that final value now on a pi five that means it will run on two cores as it would on a a pi4 and so on so we can get it running on two Calles of course Pi five's got four Calles if you were doing this on a different machine with more cores then you could you could split this up to run on more cores by dividing up the sequence uh differently again you know you could start with three at one point and then go forward a thousand terms and start another thread a thousand terms later on bit of simple math there but I done it on two just to show you the principles so there we go this is a multi-threaded method for finding the value of pi now because we're dealing with lots of fractions 1/3 1/ 15th 1 7th 1 nth and then on you know up to 10 million uh terms we're going to need more Precision more accuracy in the numbers after the the point the dot uh then you get in normal C so I'm using the gnu MP the ganu multiple Precision Library it's a portable library written in C for arbitrary Precision arithmetic on integers rational numbers and floating Point numbers open source of course been around for a long time and I'll show you how you use that uh in a moment so here is the do PI minus one there's a do pi plus one as well but it's very very similar it's just it's doing addition rather than subtraction and basically if you look at the code here what have we got we've got these mpf so that's the multiple prision floating Point numbers so that's part of that Gano Library I was just showing you about okay and basically we uh have a 1 over I because that's what we're doing 1/3 1/ 15 1 7th and basically I set that number up to be one then I divide it by the bit underneath the I and that gives us this number whatever 1/3 is in decimal whatever 1/5th is in decimal and then we just subtract them from the previous term so we keep subtracting just like that uh formula shows and I do exactly the same thing in another function but for addition so you just keep adding them and at the end you'll get a sum and then if you bring the positive the additional one and the subtraction one together then we can work out the value of pi and I'll show that here in this next slide there it is so what we do is we create two threads one thread uh runs the minus bit one thread runs the the positive bit we then wait for those two threads to finish at the very end we add them together we multiply by four and then we PR PR out the value of pi pretty simple okay demo time let's see this running on a Raspberry Pi 5 okay so here I am on my rasb Pi 5 I have the program here that I've got this code is's the do pi plus uh function very simil to the other one it's if noticed it's got the add in it rather than subtract not very long piece of code it's available in my GitHub repository and we'll just run it here now so the first two lines out are the result of the subtraction and the addition series and then you the last one is those two added together multiply by four so you get 3.14159 so there you go and then the rest of the numbers are there but they're not correct yet because it hasn't converged closer to the actual number 100 million there you can bump up that number higher and higher of course it does take longer and longer to run okay that's it my name is Gary Sims this is Gary expains I really do hope you enjoyed this fun little video if you did please do give it a thumbs up if you like these kind of videos then why not stick around by subscribing to the channel okay that's it I'll see you in the next [Music] one
Info
Channel: Gary Explains
Views: 3,252
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Calculate Pi, Leibniz formula for π, Leibniz, Pi Day, 3.14, March 14, Raspberry Pi, Infinite Series, 3.1415, How to calculate pi, Multithreaded Pi calculation, How is pi value calculated?, What is the formula to calculate digit of pi?, Calculating Pi (π), How to compute digits of pi ?, How to Calculate π, How to Calculate Pi (π), Calculate Pi with C, Pi Day 2024
Id: r5wuBbmY-lk
Channel Id: undefined
Length: 9min 57sec (597 seconds)
Published: Thu Mar 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.