Last active
August 2, 2018 07:27
-
-
Save SndrSchnklshk/5f893721b7b262467655f478d822ebd9 to your computer and use it in GitHub Desktop.
Calculate PI in C# the plain and stupid way :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Calculate PI | |
| // OUTPUT (59 seconds on my old i5 laptop): | |
| // PI = 3.14159264498239 vs 3.14159265358979 | |
| // Difference = 8.60740323460618E-09 | |
| public double CalculatePI() | |
| { | |
| double pi = 0; | |
| double sum = 1; | |
| double sqr = 0; | |
| for (double n = 2; n < 10000000000; n++) | |
| { | |
| sqr = (n * n); | |
| sum = sum + (1 / sqr); | |
| } | |
| pi = Math.Sqrt(sum * 6d); | |
| Console.WriteLine($"PI = {pi} vs {Math.PI}"); | |
| Console.WriteLine($"Difference = {Math.Abs(Math.PI - pi)}"); | |
| return pi; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment