# Square root implementations

The question was:

Write a function that calculates the root of a given number.

• Answer A = I wrote it for integers. just run up to half of the number
• Answer B+C = are for double, using Newton's law.

I just implemented this in C#.

They all work and were tested.Please comment me about anything from not well written code, complexity, style, or give a better solution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
public class FindSquareRoot
{
public FindSquareRoot()
{
int numberToFind = 49;
int res = SquareRoot(numberToFind);
Console.WriteLine(res);

double doubleNumberToFind = 157.0;
double resDouble = SquareRoot(doubleNumberToFind);
Console.WriteLine(resDouble);
resDouble = SquareRootBetter(doubleNumberToFind);
Console.WriteLine(resDouble);
}

public int SquareRoot(int number)
{
int half = number / 2;
int counter = 0;
while (counter < half)
{
if (Math.Pow(counter, 2.0) == number)
{
return counter;
}
counter++;
}
return half;
}

public double SquareRoot(double number)
{
double precision = 10e-8;
double low = 0.0;
double high = 0.0;
double prev = 0.0;
double mid = number / 2.0;

while(Math.Abs(mid - prev) > precision )
{
if(Math.Pow(mid,2) > number)
{
high = mid;
}else
{
low = mid;
}
prev = mid;
mid = (low+high)/ 2.0;
}
return mid;
}
public double SquareRootBetter(double number)
{
double precision = 10e-8;
double prev = 0.0;
double mid = number;
while(Math.Abs(mid - prev ) > precision)
{
prev = mid;
mid = (mid +(number/ mid))/2.0;
}
return mid;
}
}
}


• SquareRoot(int)

• returns half if the integer is not a perfect square. A very dubious decision. I'd consider returning a best integer approximation, or throwing an exception.

• uses a linear search. A binary search would be quite faster. A Newton-Raphson (yes it works with integers) would be even factor.

• An error approximation is better done as Math.abs(number - Math.pow(candidate_square_root, 2))

SquareRoot(int) doesn't seem to work for anything except perfect squares. Here are some examples:

 x    SquareRoot(x)  SquareRoot(x)^2
-------------------------------------
8               4               16
10               5               25
40              20              400


If your method is not intended to work with non-perfect squares, it would be a very good idea to document this.

Naming

Based on the naming guidlines methodnames should be made out of verbs or verb phrases. So e.g SquareRoot should become FindSquareRoot

Class names should be made out of nouns or noun phrases. So e.g FindSquareRoot should become SquareRootCalculator.

Constructor

The constructor of an object has the purpose to construct the object, hence the name. You shouldn't use the constructor to do the work.

You should better write tests for testing the methods instead of doing this in the constructor.

Algorithm

If you can use Math.Pow() as you already do, you can simplify this extremely by just returning Math.Pow(input, 0.5).

General

As the methods don't depend on any state of the object, you should declare them static and because all methods should be static, the class itself should be static too.

• Mathematical functions generally don't use a prefix like Find. – CodesInChaos Dec 4 '14 at 10:22