Skip to content

Instantly share code, notes, and snippets.

@SndrSchnklshk
Last active August 2, 2018 07:27
Show Gist options
  • Select an option

  • Save SndrSchnklshk/5f893721b7b262467655f478d822ebd9 to your computer and use it in GitHub Desktop.

Select an option

Save SndrSchnklshk/5f893721b7b262467655f478d822ebd9 to your computer and use it in GitHub Desktop.
Calculate PI in C# the plain and stupid way :)
// 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