ddxfish Posted August 19, 2014 Share Posted August 19, 2014 I had to make an efficiency comparison of ways to calculate Pi for a class (based on # of iterations). One is statistical and the other is algebraic. I figured I would post them here. Monte Carlo Method of calculating Pi (statistical) ;Monte Carlo method to calculate Pi $incircle = 0 $count = InputBox("Monte Carlo", "Please enter how many samples to calculate") For $i = 0 To $count $hypo = pythagoran(((Random(0, 1) * 2) - 1), ((Random(0, 1) * 2) - 1)) If $hypo <= 1 Then $incircle += 1 Next MsgBox(0, "Pi Equals", "Pi: " & ((4 * $incircle) / $count) & @CRLF & "Samples: " & $count) Func pythagoran($a, $b) Return Sqrt(($a ^ 2) + ($b ^ 2)) EndFunc ;==>pythagoran Nilakantha Method of calculating Pi (algebraic) ;Nilakantha method to calc pi $pi = 3 $count = InputBox("Nilakantha", "Enter number of iterations") For $i = 1 to $count $denom = (2 * $i) + 2 if mod($i, 2) = 1 Then $pi += (4 / (($denom)*($denom - 1)*($denom -2))) elseif mod($i, 2) = 0 Then $pi -= (4 / (($denom)*($denom - 1)*($denom -2))) EndIf Next MsgBox(0, 0, $pi) Link to comment Share on other sites More sharing options...
wakillon Posted August 20, 2014 Share Posted August 20, 2014 As AutoIt is limited with a 15 digits precision, ACos(-1) stay sufficient ! PlayHD and mLipok 2 AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now