// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class Program { private const double PREC = 0.00000001; public static double MySqrt(double x) { double lower = x * (1 - PREC); double upper = x * (1 + PREC); double res = 0.05*x; do { res = 0.5*(res + x/res); } while(res*res < lower || res*res > upper); return res; } private const int N = 1000000; public static void Main(string[] args) { Console.WriteLine(Math.Sqrt(Math.PI)); Console.WriteLine(MySqrt(Math.PI)); DateTime dt1 = DateTime.Now; for(int i = 0; i < N; i++) { if(Math.Sqrt(i) < 0) throw new Exception("Impossible"); } DateTime dt2 = DateTime.Now; Console.WriteLine("Math.Sqrt: " + (dt2 - dt1).TotalSeconds); DateTime dt3 = DateTime.Now; for(int i = 0; i < N; i++) { if(MySqrt(i) < 0) throw new Exception("Impossible"); } DateTime dt4 = DateTime.Now; Console.WriteLine("MySqrt: " + (dt4 - dt3).TotalSeconds); } } }