Design an algorithm to find the square root of a positive number by starting with the number itself as the first guess and repeatedly producing a new guess from the previous one by averaging the previous guess with the result of dividing the original number by the previous guess. Analyse the control of this repetitive process. In particular, what condition should terminate the repetition?
I think this is the first video of those SICP lectures or somewhere in the SICP book :) nice
I made a quick console application in C# that does what You asked. Hope it helps: double v = Convert.ToDouble(Console.ReadLine()); // the original number double sqr = Math.Sqrt(v); // the square root of the number double prevGuess = v; //the previous guess while (prevGuess != sqr) // this loop iterates until the square root of the number is found prevGuess = (prevGuess + (v / prevGuess)) / 2; Console.Write(prevGuess); //write the result
Join our real-time social learning platform and learn together with your friends!