Calculating Pi in Hopscotch



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.


Happy Pi Day! π Suuuper Celebraτion! -- Happy Tau Day! 6.28

Wow! I'm gonna read this all in a second. I was trying to calculate it using tangents, but I can't seem to make it work...


That's actually a good idea. The only way I know of, though, is using Machins Formula:

Which uses inverse trig instead of regular trig, which Hopscotch doesn't have :confused:


Yeah, that's when I got stuck. I also tried coding the "dropping sticks" meathod, but it's really inaccurate unless you run it a lot.


And that method, along with the trig method, also comes back to the problem of Hopscotch only supporting up to 5 decimal points.


Actually, @justanerd has a project that uses rotation as a value, and it goes up to 16 digits! Here's the link:

I don't know if it's what you're looking for, but it's pretty cool!


Wow, that's awesome! I just added that 'rotation as a value' thing into my project and it works amazingly! (Calculated to 16 digits in just under 2 seconds :D) But, that is kind of a half answer. We want infinite pi, not 16 digits!


Hmm... It would be a challenge to link multiple texts together, and it would be innefective, and not infinite.


What I'm saying is have one text object that calculates each digit and then makes a clone of itself that sets it's text to that digit then places itself next to the previous digit. It could theoretically go on forever. The placement of each text and stuff is the easy part, calculating each digit is the hard part.


Yeah, because it would be segmented off. I can't seem to find any calculations that we would be able to use...


there is an equation of find pi to X digit, without the previous digits, you could have each text object hold 16 digits, and use the equation to calculate for the 17, 33, 49, etc digits, and then work from there, but the equation is more than three times a long as what you have up and is 90% Greek letters

(note: said equation is also really hard to find and I didn't bookmark the page, that's why there isn't a photo here)


Dang guys, we just had 8 pi themed features, two of them @justanerd's and mine!


That's the exact type of equation I'm looking for. I've researched everywhere, but can't find any that calculate in base 10.


I'd just like to take a moment:

@awesomeonion @Liza @asha Arbitrary precision. Please. I know Swift and Objective-C both have it, and I feel like you just put that limit to troll us xD

Edit: Before the flaggers come, this is on topic as arbitrary precision would allow as much pi as we want. And who doesn't love pi?


I am so sorry for not bookmarking the page, but I think I could come up with a script that can convert base 16 to base 10 if you can find a way to segment the variables into 1 place a piece, (12345 becomes 1+2x10*3x100) then I could take the segmented pieces and program a base 16 multiplication algorithm to change 3x100 to 18x16 12 then to 1x256 + 2x16 + 12, (plus separates digits)

also: I'M FEATURED!?!?!


@BuildASnowman, I believe that there may be a way. I'm experimenting.

Also, my pi project seems the most accurate... Interesting! :laughing:


See, that's the problem. You could probably do it with some sort of brute force algorithm or even in O(n^2) time if you had strings, but currently there is no way to store the mathematical expressions without evaluating them.


Please keep us updated! What have you found?


why did they feature meh worst one? ;-; (Nilakantha I have forsaken you ;-; )

pls nerrr that was the only one in prototype xD (I don't mind the feature, I just mind that feature)


I'm actually fairly certain you could ask @Liza or @Asha to put a different one in!

@BuildASnowman, I'm not sure if it'll work. Not a lot of progress yet, so I don't want to share the meathod. Failing epically so far.