Write a program to calculate the Dottie number. This is the number you get when you type any number into a scientific calculator and then repeatedly press the

`cos`

button, with the calculator set to radians. The number displayed updates, getting closer and closer to a certain number, and eventually stops changing.

`cos`

here is the trigonometric function cosine, but you don’t need to know any trigonometry, or what cosine means, for this challenge. Just do the same thing you would with a handheld calculator: take cosine over and over again until you get the answer.

My solution is below the fold (I didn’t do optional challenge #3, mostly because real work had to be done before I got to it).

static void Main(string[] args) { double fixedPt = 0; int tries = 0; tries = Dottie(23, Math.Cos, out fixedPt); Console.WriteLine("Starting at {0}, fixed point was found at {1} after {2} tries for Cos(x).", "23", Math.Round(fixedPt,5).ToString(), tries.ToString()); tries = Dottie(2, x => x - Math.Tan(x), out fixedPt); Console.WriteLine("Starting at {0}, fixed point was found at {1} after {2} tries for 1 - Tan(x).", "2", Math.Round(fixedPt, 5).ToString(), tries.ToString()); tries = Dottie(1.5, x => 1 + (1 / x), out fixedPt); Console.WriteLine("Starting at {0}, fixed point was found at {1} after {2} tries for 1 + 1/x", "1.5", Math.Round(fixedPt, 5).ToString(), tries.ToString()); Console.ReadKey(); } static private int Dottie(double curVal, Func]]>f, out double fixedPt) { int tries = 0; const int quitAfter = 1000; double newVal = curVal + 1; while ((tries < quitAfter) && (Math.Round(curVal, 10) != Math.Round(newVal,10))) { tries++; curVal = newVal; newVal = f(curVal); } fixedPt = newVal; return tries; }