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?

lgrov44 wrote:

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?

-MePix- wrote:

lgrov44 wrote:

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!
No worries.
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?

-MePix- wrote:

(#3)

lgrov44 wrote:

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!
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.

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:

setpito3setito2foreverchangepiby4*i*i+1*i+2-i+2*i+3*i+4changeiby4

Last edited by mumu245 (March 28, 2024 14:51:39)

MrKingofScratch
Scratcher
100+ posts

How to calculate Pi?

Shrek_em
Scratcher
34 posts

How to calculate Pi?

circumference / diameter
lgrov44
Scratcher
500+ posts

How to calculate Pi?

mumu245 wrote:

-MePix- wrote:

(#3)

lgrov44 wrote:

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!
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.
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.
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:
atanof

Last edited by PhiPhenomenon (March 30, 2024 01:31:27)

-MePix-
Scratcher
24 posts

How to calculate Pi?

mumu245 wrote:

Even though Scratch isn't precise enough, you can try this algorithm:

setpito3setito2foreverchangepiby4*i*i+1*i+2-i+2*i+3*i+4changeiby4
Can you make a project with the script? It is kinda hard to tell what it is through forums
BigNate469
Scratcher
1000+ posts

How to calculate Pi?

Shrek_em wrote:

circumference / diameter
The 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
46 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.

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.
london11gm
Scratcher
42 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

Last edited by london11gm (May 26, 2024 19:10:24)

Vaibhs11
Scratcher
1000+ posts

How to calculate Pi?

london11gm wrote:

22/7 = pi
That is a terrible estimation of pi compared to what OP's asking for.
TheUltimateHoodie
Scratcher
1000+ posts

How to calculate Pi?

Vaibhs11 wrote:

london11gm wrote:

22/7 = pi
That is a terrible estimation of pi compared to what OP's asking for.
but my teacher said to use that

did my teacher lie to me?

Last edited by TheUltimateHoodie (March 29, 2024 10:06:07)

PhiPhenomenon
Scratcher
500+ posts

How to calculate Pi?

TheUltimateHoodie wrote:

but my teacher said to use that

did my teacher lie to me?
The 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.
TheUltimateHoodie
Scratcher
1000+ posts

How to calculate Pi?

PhiPhenomenon wrote:

TheUltimateHoodie wrote:

but my teacher said to use that

did my teacher lie to me?
The 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.
so…my teacher lied…she said 22/7 WAS pi

Powered by DjangoBB