niklas-heer / speed-comparison

A repo which compares the speed of different programming languages.
https://niklas-heer.github.io/speed-comparison
MIT License
475 stars 76 forks source link

C# .Net implementation #28

Closed niklas-heer closed 1 year ago

niklas-heer commented 1 year ago

From #27 by @PauloCotrim

using System.IO;

var data = String.Empty;

try
{
data = File.ReadAllText("rounds.txt", System.Text.Encoding.UTF8);
}
catch (IOException err)
{
Console.WriteLine($"Couldn't read file:\n {err.Message}");
}

int rounds = int.Parse(data.Replace("\n", "").Replace("\r", ""));

double pi = 1;
double x = 1;

for (int i = 2; i < rounds + 2; i++)
{
x *= -1;
pi += (x / (2 * i - 1));
}

pi *= 4;
Console.WriteLine(pi);