Calculating Square Root

Without Using Library Functions

Page 1 of 1

11 Replies - 8379 Views - Last Post: 10 August 2010 - 03:21 PM Rate Topic: -----

#1 adhish94  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 12-July 10

Calculating Square Root

Posted 10 August 2010 - 03:10 AM

Calculate the square root of a number without using any library function. Here's my code:


import java.io.*;
class squareRoot
    {
        public static void main(String args[])throws IOException
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter number");
            int n = Integer.parseInt(br.readLine());
            int sq = 0;
            for(int i=1; i<(n/2); i++)
            {
                if((i*i) == n)
                {
                    sq = i;
                    break;
                }
            }
            if(sq == 0) System.out.println("Not a perfect square");
            else System.out.println("Square root = " + sq);
        }
    }




But this would calculate the square root of a perfect square like 25 or 36 only. So how should I write some code which can calculate the square root of any number?

Maybe I could use double type variables for i (loop variable) and sq, and increase i by something like 0.001. But this would make the program highly ineffiecient and time-consuming and still incompetent for some numbers.

This post has been edited by adhish94: 10 August 2010 - 03:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Calculating Square Root

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,150
  • Joined: 11-December 07

Re: Calculating Square Root

Posted 10 August 2010 - 04:00 AM

Using a double is a good move. Look up Newton's method. In a nutshell, knows the square root is between 0 and the number so it makes an initial guess at the middle. Now it knows it's between the middle and one of the previous bounds. Keep on going until you have the precision you want.
Was This Post Helpful? 1
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 05:42 AM

Here's a quick and easy little equation you could follow:

-Get the number(n) you want to find the sqrt of
-Plug it in to this equation x = .5 * ((x+n) / x) where x starts at 1
-Do that equation for however big the number you're finding the sqrt of is

And yes, remember to use doubles for sure!


Hope this helps!

This post has been edited by eZACKe: 10 August 2010 - 05:43 AM

Was This Post Helpful? 1
  • +
  • -

#4 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,716
  • Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 06:06 AM

But x = .5* would mean he'd have to use the math library for the *.

I found this site, might help. This too

This post has been edited by NeoTifa: 10 August 2010 - 06:11 AM

Was This Post Helpful? 1
  • +
  • -

#5 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 06:16 AM

View PostNeoTifa, on 10 August 2010 - 09:06 AM, said:

But x = .5* would mean he'd have to use the math library for the *.

I found this site, might help. This too


Oh? I thought he was just referring to the Methods defined in the Math library.
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,716
  • Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 06:20 AM

Wait, did you mean * to be a variable? XD
Was This Post Helpful? 0
  • +
  • -

#7 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 06:25 AM

No haha I just meant it to be multiplication. I thought he just wanted an alternate way of doing it without using sqrt(). I was under the assumption that being unable to use that method was his only limitation.
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,716
  • Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 06:58 AM

Whoops, I missed the whole parenthesis jumble. XD Sorry.
Was This Post Helpful? 0
  • +
  • -

#9 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 07:07 AM

Not a problem. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculating Square Root

Posted 10 August 2010 - 03:16 PM

View PostNeoTifa, on 10 August 2010 - 07:06 AM, said:

But x = .5* would mean he'd have to use the math library for the *.

I found this site, might help. This too

Woldn't consider * as being in the Math library actually it is not ?
Figuring out a sqrt without the * operator would be a real challenge using iterations but aren't + and - in the Math lib too ? :)
Was This Post Helpful? 0
  • +
  • -

#11 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,716
  • Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 03:18 PM

Lol I thought he was using it as some type of variable. Was skimming. Lol trying to do this without java.lang would be epic ;)
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculating Square Root

Posted 10 August 2010 - 03:21 PM

View PostNeoTifa, on 10 August 2010 - 04:18 PM, said:

Lol I thought he was using it as some type of variable. Was skimming. Lol trying to do this without java.lang would be epic ;)

If you want to try, I'll be glad to help ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1