import 'dart:io'; // Calculate arctan(1/x) using Taylor series // arctan(1/x) = 1/x - 1/(3*x^3) + 1/(5*x^5) - ... BigInt arctan(int x, int decimals) { BigInt scale = BigInt.from(10).pow(decimals + 10); BigInt xBig = BigInt.from(x); BigInt xSquared = xBig * xBig; BigInt term = scale ~/ xBig; BigInt result = BigInt.zero; int n = 0; while (term != BigInt.zero && n < decimals * 3) { BigInt divisor = BigInt.from(2 * n + 1); BigInt contrib = term ~/ divisor; if (n % 2 == 0) { result += contrib; } else { result -= contrib; } term = term ~/ xSquared; n++; } return result; } BigInt calculatePi(int decimals) { BigInt atan1_5 = arctan(5, decimals); BigInt atan1_239 = arctan(239, decimals); // pi = 16*arctan(1/5) - 4*arctan(1/239) return atan1_5 * BigInt.from(16) - atan1_239 * BigInt.from(4); } void main(List args) { int decimals = 100; if (args.isNotEmpty) { try { decimals = int.parse(args[0]); if (decimals < 1) decimals = 100; } catch (e) { decimals = 100; } } BigInt pi = calculatePi(decimals); String piStr = pi.toString(); // Print with decimal point stdout.write('3.'); int startIndex = 1; int endIndex = startIndex + decimals; if (endIndex > piStr.length) { endIndex = piStr.length; } stdout.write(piStr.substring(startIndex, endIndex)); // Pad with zeros if needed for (int i = 0; i < decimals - (endIndex - startIndex); i++) { stdout.write('0'); } print(''); }