This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-27 15:31:19

skippito
Scratcher
Registered: 2012-03-17
Posts: 100+

Pi calculator

I need a script that calculates pi.

Offline

 

#2 2012-05-27 16:10:39

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Pi calculator

You should check out the Wikipedia page.  There are many algorithms for calculating Pi.  My favorite is this the Gauss-Legendre Algorithm.  Here's how it works:

set [Accuracy v] to [50] // Choose a big number.
set [a.n v] to [1]
set [b.n v] to ([1]/( [sqrt v] of [2]))
set [t.n v] to [0.25]
set [p.n v] to [1]
repeat (Accuracy)
set [a.n+1 v] to (((a.n) + (b.n)) / [2])
set [b.n+1 v] to ([sqrt v] of ((a.n) * (b.n)))
set [t.n+1 v] to ((t.n) - ( (p.n) * ( ( (a.n) - (a.n+1) ) * ( (a.n) - (a.n+1) ) ) ))
set [p.n+1 v] to ([2] * (p.n))
set [a.n v] to (a.n+1)
set [b.n v] to (b.n+1)
set [t.n v] to (t.n+1)
set [p.n v] to (p.n+1)
end
set [Pi v] to ((((a.n) + (b.n)) * ((a.n) + (b.n))) / ([4] * (t.n)))

Offline

 

#3 2012-05-27 16:58:02

fuz50
Scratcher
Registered: 2008-09-19
Posts: 100+

Re: Pi calculator

I'm not sure what you need it for, but if you don't need it this accurate, 355/113 is pretty darn close up to the 7th decimal place.


http://img10.imageshack.us/img10/7404/cooltext529102298.jpg
http://siggen.toontown-click.de/sig1.pnghttp://siggen.toontown-click.de/sig2.png

Offline

 

#4 2012-05-27 17:32:57

sanjayraj
Scratcher
Registered: 2012-03-25
Posts: 500+

Re: Pi calculator

You could do something like this:

when gf clicked
delete (all) of (pi)
add (thing) to Pi
set (PI) to (0)
set (Fractions) to 1
forever
set (PI) to (PI) + (4) / (Fractions)
change (Fractions) by (2)
set (PI) to (PI) - 4 / (Fractions)
change (Fractions) by (2)
say (Pi)
replace item (1) of (Pi) with (PI)


http://i46.tinypic.com/23sw40j.png

Offline

 

#5 2012-05-27 17:50:55

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Pi calculator

sanjayraj wrote:

You could do something like this:

when gf clicked
delete (all v) of [Pi v]
add [thing] to [Pi v]
set [Pi v] to [0]
set [Fractions v] to [1]
forever
set [Pi v] to (((Pi) + (4)) / (Fractions))
change [Fractions v] by (2)
set [Pi v] to (((Pi) - (4)) / (Fractions))
change [Fractions v] by (2)
say (Pi)
replace item (1) of [Pi v] with (Pi)

Fixed your scripts.

Last edited by amcerbu (2012-05-27 23:06:31)

Offline

 

#6 2012-05-27 19:04:53

sanjayraj
Scratcher
Registered: 2012-03-25
Posts: 500+

Re: Pi calculator

Ok, thanks for fixing my scripts!


http://i46.tinypic.com/23sw40j.png

Offline

 

#7 2012-05-28 06:20:02

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Pi calculator

I have a Pi calculating project, if none of these work. The scripts there might help.

Last edited by Splodgey (2012-05-28 06:20:35)

Offline

 

#8 2012-05-28 14:25:39

skippito
Scratcher
Registered: 2012-03-17
Posts: 100+

Re: Pi calculator

amcerbu wrote:

You should check out the Wikipedia page.  There are many algorithms for calculating Pi.  My favorite is this the Gauss-Legendre Algorithm.  Here's how it works:

set [Accuracy v] to [50] // Choose a big number.
set [a.n v] to [1]
set [b.n v] to ([1]/( [sqrt v] of [2]))
set [t.n v] to [0.25]
set [p.n v] to [1]
repeat (Accuracy)
set [a.n+1 v] to (((a.n) + (b.n)) / [2])
set [b.n+1 v] to ([sqrt v] of ((a.n) * (b.n)))
set [t.n+1 v] to ((t.n) - ( (p.n) * ( ( (a.n) - (a.n+1) ) * ( (a.n) - (a.n+1) ) ) ))
set [p.n+1 v] to ([2] * (p.n))
set [a.n v] to (a.n+1)
set [b.n v] to (b.n+1)
set [t.n v] to (t.n+1)
set [p.n v] to (p.n+1)
end
set [Pi v] to ((((a.n) + (b.n)) * ((a.n) + (b.n))) / ([4] * (t.n)))

Thanks! I used your script and it worked perfectly. You can find the final product here: http://scratch.mit.edu/projects/skippito/2569922

Offline

 

#9 2012-05-30 02:33:09

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Pi calculator

I'm glad it worked out for you!  That particular pattern converges very quickly.  Unfortunately, Scratch floating-point values have a small number of digits after the decimal...

Offline

 

#10 2012-05-30 18:58:19

likegames
Scratcher
Registered: 2008-03-24
Posts: 100+

Re: Pi calculator

Here is my (very slow) pi calculator:

when gf clicked
delete (all v) of [pil v]
add [4] to [pil v]
set [no v] to (1)
set [pi v] to (4)
set [+/- v] to (0)
forever
    replace item (1 v) of [pil v] with (pi)
    if <(+/-) = (0)>
        change [pi v] by (((4) /(no))*(-1))
        set [+/- v] to (1)
    else
        change [pi v] by ((4) /(no))
        set [+/- v] to (0)
    end
    change [no v] by (2)
You can find the (very slow) calculator on my profile.
It uses the formula (4/1)-(4/3)+(4/5)-(4/7)+(4/9) and so on.

Last edited by likegames (2012-05-30 19:02:22)


There are 10 types of people in this world: Those who understand trinary, those who don't, and those who think this is the same old signature.
Three lines! (and base three!)

Offline

 

Board footer