Treat Code as Data: C#: Implement Hero Roots Imperatively: Listing 3
This imperative implementation of Hero's square root algorithm concentrates on how to calculate the root. It contains loops and checks, mixed with algorithms.
public static double HeroRoot(double square, double epsilon)
double lastGuess = 1;
double guess = ((square / lastGuess + lastGuess) / 2);
while (Math.Abs(lastGuess - guess) > epsilon)
lastGuess = guess;
guess = ((square / lastGuess + lastGuess) / 2);