Discuss Scratch
- -MePix-
-
Scratcher
24 posts
How to calculate Pi?
How to calculate Pi
How do I calculate Pi accurately with scratch blocks?
I am making a Pi game where the digits go on forever
- lgrov44
-
Scratcher
500+ posts
How to calculate Pi?
There are many ways to do this. For your example, try just downloading and saving the digits, however, if a script is what you need, you can try the long script from this project.
- -MePix-
-
Scratcher
24 posts
How to calculate Pi?
There are many ways to do this. For your example, try just downloading and saving the digits, however, if a script is what you need, you can try the long script from this project.Thanks!
- lgrov44
-
Scratcher
500+ posts
How to calculate Pi?
No worries.There are many ways to do this. For your example, try just downloading and saving the digits, however, if a script is what you need, you can try the long script from this project.Thanks!
- NIKI-KOLCHAGOV
-
Scratcher
500+ posts
How to calculate Pi?
This topic should probably be closed since it looks resolved
- mumu245
-
Scratcher
1000+ posts
How to calculate Pi?
(#3)That doesn't compute pi. It renders a fixed list of digits. Scratch isn't precise enough to hold more than 9 or 10 digits of pi.There are many ways to do this. For your example, try just downloading and saving the digits, however, if a script is what you need, you can try the long script from this project.Thanks!
Last edited by mumu245 (March 28, 2024 14:42:07)
- mumu245
-
Scratcher
1000+ posts
How to calculate Pi?
Even though Scratch isn't precise enough, you can try this algorithm:
set [pi v] to [3]
set [i v] to [2]
forever
change [pi v] by ((4) * (((i) * (((i) + (1)) * ((i) + (2)))) - (((i) + (2)) * (((i) + (3)) * ((i) + (4))))))
change [i v] by (4)
end
Last edited by mumu245 (March 28, 2024 14:51:39)
- MrKingofScratch
-
Scratcher
100+ posts
How to calculate Pi?
here's a funny approach
https://scratch.mit.edu/projects/682780554/
https://scratch.mit.edu/projects/682780554/
- lgrov44
-
Scratcher
500+ posts
How to calculate Pi?
What do you mean? You can store that many digits. You would, at most, save it as an item of a list, but you can definitely store all of those digits, specifically, you can definitely store more than the mentioned 9 or 10 digits.(#3)That doesn't compute pi. It renders a fixed list of digits. Scratch isn't precise enough to hold more than 9 or 10 digits of pi.There are many ways to do this. For your example, try just downloading and saving the digits, however, if a script is what you need, you can try the long script from this project.Thanks!
- PhiPhenomenon
-
Scratcher
500+ posts
How to calculate Pi?
There's a pretty cool infinite sum that can be used to calculate pi:

However, it's pretty slow. You have to sum like the first thousand terms of the terms to get like the first four places of pi.
There's a faster one called the Machin formula, but it requires a little more math:

You can find the arctangent block from the operators section:

However, it's pretty slow. You have to sum like the first thousand terms of the terms to get like the first four places of pi.
There's a faster one called the Machin formula, but it requires a little more math:

You can find the arctangent block from the operators section:
([atan v] of ())
Last edited by PhiPhenomenon (March 30, 2024 01:31:27)
- -MePix-
-
Scratcher
24 posts
How to calculate Pi?
Even though Scratch isn't precise enough, you can try this algorithm:Can you make a project with the script? It is kinda hard to tell what it is through forumsset [pi v] to [3]
set [i v] to [2]
forever
change [pi v] by ((4) * (((i) * (((i) + (1)) * ((i) + (2)))) - (((i) + (2)) * (((i) + (3)) * ((i) + (4))))))
change [i v] by (4)
end
- BigNate469
-
Scratcher
1000+ posts
How to calculate Pi?
circumference / diameterThe issue with that is to know both the circumference and diameter you have to know Pi and all of its digits, and Pi is indefinitely long…
- homewins
-
Scratcher
47 posts
How to calculate Pi?
https://scratch.mit.edu/projects/47680290/ is the fastest Scratch pi calculator I know of.
If you really wanted to you could even use mine: https://scratch.mit.edu/projects/991482978/
2000 digits in ~3 seconds on @DadOfMrLog's ~112 seconds on mine, so mine is 40x slower. Mine also scales worse. I hope I can make mine better.
Basically the idea behind both it is Machin-like formulas, of the form
pi/4=a*arctan(1/x)+b*arctan(1/y)…etc…
Using infinite series to calculate each arctan term.
The hard part is figuring out how to make custom blocks to do your arithmetic with thousands of digits of precision.
If you really wanted to you could even use mine: https://scratch.mit.edu/projects/991482978/
2000 digits in ~3 seconds on @DadOfMrLog's ~112 seconds on mine, so mine is 40x slower. Mine also scales worse. I hope I can make mine better.
Basically the idea behind both it is Machin-like formulas, of the form
pi/4=a*arctan(1/x)+b*arctan(1/y)…etc…
Using infinite series to calculate each arctan term.
The hard part is figuring out how to make custom blocks to do your arithmetic with thousands of digits of precision.
Last edited by homewins (March 29, 2024 02:09:32)
- Vaibhs11
-
Scratcher
1000+ posts
How to calculate Pi?
This is a wikipedia article on different formulae that can be used to approximate pi. Here is a list of just the formulas (ignore the integrals part). You can implement any of these into scratch, although the more efficient ones take more computing power or are prone to floating point errors, and the less efficient ones just take more time to get there.
Floating point numbers aren't precise enough for any of these formulas. So, I recommend you store the numbers as a string and make your own digit-by-digit addition, subtraction, long multiplication and long division functions for strings. Using strings can hold way more numbers than floating point can I think.
Floating point numbers aren't precise enough for any of these formulas. So, I recommend you store the numbers as a string and make your own digit-by-digit addition, subtraction, long multiplication and long division functions for strings. Using strings can hold way more numbers than floating point can I think.
- london11gm
-
Scratcher
48 posts
How to calculate Pi?
22/7 = (edit) roughly pi
You can also check out @DadOfMrLog 's super pi calculator
https://scratch.mit.edu/projects/47680290
You can also check out @DadOfMrLog 's super pi calculator
https://scratch.mit.edu/projects/47680290
Last edited by london11gm (May 26, 2024 19:10:24)
- Vaibhs11
-
Scratcher
1000+ posts
How to calculate Pi?
22/7 = piThat is a terrible estimation of pi compared to what OP's asking for.
- TheUltimateHoodie
-
Scratcher
1000+ posts
How to calculate Pi?
but my teacher said to use that22/7 = piThat is a terrible estimation of pi compared to what OP's asking for.
did my teacher lie to me?
Last edited by TheUltimateHoodie (March 29, 2024 10:06:07)
- PhiPhenomenon
-
Scratcher
500+ posts
How to calculate Pi?
but my teacher said to use thatThe OP wants an approximation of pi that goes on as long as they can. 22/7 comes out to about 3.1428. That's a 0.0403% error from the actual value of pi, so it's fine to use in calculations. However, it's 3rd decimal place is already wrong.
did my teacher lie to me?
- TheUltimateHoodie
-
Scratcher
1000+ posts
How to calculate Pi?
so…my teacher lied…she said 22/7 WAS pibut my teacher said to use thatThe OP wants an approximation of pi that goes on as long as they can. 22/7 comes out to about 3.1428. That's a 0.0403% error from the actual value of pi, so it's fine to use in calculations. However, it's 3rd decimal place is already wrong.
did my teacher lie to me?











