using System; using System.Numerics; class Program { static void Main(string[] args) { int decimals = 100; if (args.Length > 0) { if (!int.TryParse(args[0], out decimals) || decimals <= 0) decimals = 100; } BigInteger pi = CalculatePi(decimals); Console.WriteLine(FormatPi(pi, decimals)); } // Calculate arctan(1/x) using Taylor series static BigInteger Arctan(int x, int decimals) { BigInteger scale = BigInteger.Pow(10, decimals + 10); BigInteger xBig = new BigInteger(x); BigInteger xSquared = xBig * xBig; BigInteger result = BigInteger.Zero; BigInteger term = scale / xBig; int n = 0; while (term != 0 && n < decimals * 3) { BigInteger divisor = new BigInteger(2 * n + 1); BigInteger contrib = term / divisor; if (n % 2 == 0) result += contrib; else result -= contrib; term /= xSquared; n++; if (n > decimals * 2) break; } return result; } // Calculate pi using Machin's formula static BigInteger CalculatePi(int decimals) { BigInteger atan1_5 = Arctan(5, decimals); BigInteger atan1_239 = Arctan(239, decimals); return 16 * atan1_5 - 4 * atan1_239; } // Format pi with decimal point static string FormatPi(BigInteger pi, int decimals) { string piStr = pi.ToString(); while (piStr.Length < decimals + 10) piStr = "0" + piStr; return "3." + piStr.Substring(1, decimals); } }