14

how can I create a method that returns the sqrt of a given nunber?

For example: sqrt(16) returns 4 and sqrt(5) returns 2.3 ...
I am using Java and know the Math.sqrt() API function but I need the method itself.

2
  • 2
    The only reason I can think is homework, so I've suggested thisin the tags.
    – David M
    Jun 16, 2010 at 8:12
  • Read the "Numerical Recipes in C" book
    – Fanatic23
    Jun 17, 2010 at 17:46

5 Answers 5

12

Java program to find out square root of a given number without using any Built-In Functions

public class Sqrt
{

  public static void main(String[] args)
  {
    //Number for which square root is to be found
    double number = Double.parseDouble(args[0]);

    //This method finds out the square root
    findSquareRoot(number);

}

/*This method finds out the square root without using
any built-in functions and displays it */
public static void findSquareRoot(double number)
{

    boolean isPositiveNumber = true;
    double g1;

    //if the number given is a 0
    if(number==0)
    {
        System.out.println("Square root of "+number+" = "+0);
    }

    //If the number given is a -ve number
    else if(number<0)
    {  
        number=-number;
        isPositiveNumber = false;
    }

    //Proceeding to find out square root of the number
    double squareRoot = number/2;
    do
    {
        g1=squareRoot;
        squareRoot = (g1 + (number/g1))/2;
    }
    while((g1-squareRoot)!=0);

    //Displays square root in the case of a positive number
    if(isPositiveNumber)
    {
        System.out.println("Square roots of "+number+" are ");
        System.out.println("+"+squareRoot);
        System.out.println("-"+squareRoot);
    }
    //Displays square root in the case of a -ve number
    else
    {
        System.out.println("Square roots of -"+number+" are ");
        System.out.println("+"+squareRoot+" i");
        System.out.println("-"+squareRoot+" i");
    }

  }
}
11

You will probably have to make use of some approximation method.

Have a look at

Methods of computing square roots

0
9

This version uses Newton's Method, the most common method of calculating sqrt, and won't check that the input is actually an integer, but it should solve your problem just fine.

int num = Integer.parseInt(input("Please input an integer to be square rooted."));
while(0.0001 < Math.abs(guess * guess - num)){
    guess = (guess + num / guess) / 2;
}
output(Integer.toString(guess));

The second line checks how close the current guess is to the true result, and if close enough breaks the loop. The third line uses Newton's Method to get ever-closer to the true value of the sqrt. I hope this helps. :)

7

Here's something to think about:

To find a square root, you simply need to find a number which, raised to the power of 2 (although just multiplying by itself is a lot easier programmatically ;) ) gives back the input.

So, start with a guess. If the product is too small, guess larger. If the new product is too large, you've narrowed it down - guess somewhere in between. You see where I'm going...

Depending on your need of precision and/or performance, there are of course lots of ways. The solution hinted at in this post is in no way the best one in either of those categories, but it gives you a clue on one way to go.

2
  • That will take lot of processing though!
    – ivorykoder
    Oct 11, 2010 at 10:35
  • @ivorykoder: Sure it will. But there are lots of methods to choose the next guess intelligently, in order to rapidly shrink the interval size - mostly it boils down to being able to exclude as much of the remaining interval as possible, regardless of which side of the guess the answer is at. Feb 2, 2015 at 8:31
5

One that I invented (or reinvented if the case may be) is this:

Next Guess = ( ( Guess2) + N ) / ( 2 × Guess )

Example:

Square root of 10, first guess is, let's say, 10:

Guess1 = (100+10)/20=5.5

Guess2 = (30.25+10)/(2*5.5)= 3.6590909090...

Guess3 = (13.3889+10)/(3.65909090*2)=3.196005082...

etc.

Gets you 3.16227766... or thereabouts.

This is actually a simplified version of my original method

Guess + ( ( N + Guess2 ) / ( 2 × Guess ) )

which looks an awful lot like Bakhshali's method.

0