Happy Pi Day everyone!
I'm here with a question - how, in Hopscotch, can we calculate Pi? Now, I thought this would be an easily answered question when I first came upon it. There are plenty of awesome formulas for doing just this! So, I implemented a pretty simple one in Hopscotch:
But, upon running this, I got the result:
Which is part of pi, but certainly is not a big part of pi. Our problem is that Hopscotch only supports up to 5 decimal places. So, I did some thinking, and realized that while Hopscotch doesn't support big decimals, it does support big numbers. So, why can't we just multiply the number we get by 10^n to get rid of the decimal point? Upon trying that, I facepalmed once again. The formula, or any other common formulas I know of, doesn't generate digits at a constant and consistent rate, so you end up with a bunch of zeroes at the end or (usually) a giant part of the number cut off still by the fixed precision (how many decimals Hopscotch can handle).
Here, I started resorting to desperate measures (Although desperate measures usually mean really awesome math, and this time is no exception) Using the iterative function:
Does anyone recognize that? That's right, it's the formula for the Mandelbrot set! But that doesn't matter to Pi. That's just a cool fact. Anyways, taking that formula and starting at 0, if c = 1/4 then the sequence slowly goes towards 1/2. But, if it is any number bigger than 1/4th, it slowly goes to infinity. Then we ask ourselves the question: How many times do we need to iterate that equation with c = 1/4+n before we get a number over 2, for some choices of n? Well, the answer is suprsingly:
If n is 0.01, you need 31 iterations
If n is 0.0001, you need 314 iterations
If n is 0.000001, you need 3141 iterations
Do you see the pattern? Each time you add an extra two zeroes, an extra digit of pi gets added to the number of iterations! Now, you might be asking at this point: "Why, BAS, did you use such a complicated way to calculate pi?" Well, first off, it was really cool and I still don't fully know why it works and wanted to see if it worked, and secondly, it was the only way I knew to calculate the digits of pi at a constant rate in an integer. But, it is incredibly slow, and aftera painful wait, Hopscotch gave me the following number:
3145 e+20 (Or something like that, don't have my ipad rn so I can't check)
That's right, for big numbers Hopscotch's goes into scientific notation. I just about ripped my hair out right there.
But... there was still hope. I stumbled upon Spigot Algorithms. These are formulas that allow you to calculate Pi one digit at a time, and using clones for each digit, you could implement one of these in Hopscotch! So I tried - and I couldn't. I started with the Bailey-Borwein-Plouffe formula:
However, after implementing this in another language first (to test if I had the right formula) I found that it kept giving me the number:
This, after some research, is because the BBP formula calculates Pi in base 16, and since I don't think there is a way to convert between bases in Hopscotch I don't think we can use this formula. The only hope I have left is the Rabinowitz-Wagon Spigot Algorithm which seems to be in base 10. I have had considerable trouble implementing this algorithm in Hopscotch, and that is why I am here. Would it be possible to implement this, any other spigot algorithms (Bounded or unbounded, it doesn't matter), or if you can find a way, any sort of algorithm for calculating Pi in Hopscotch?
NOTE: I know a lot of you won't understand this post, and that is completely fine. But hopefully it will be a great learning experience for those who want to learn about this kind of stuff and a fun challenge for those who already do.