Main Topics
Browse All Topicshow to find square root of a number without using sqrt function
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Try this link.
http://www.csd.uwo.ca/cour
Hi ashu1004,
Most square root algorithms are based on long division and require a series of guesses. Just as the links above demonstrate, a series of guesses determines the correct value for a given digit, a bit of simple math is performed, and the guessing mechanism is repeated for the next digit. Usually, the guesswork is reduced to the point that each guess is instinctive, but programs will have to continue the guesswork, or be table driven to eliminate guessing. (i.e. You and I both just "know" that the square root of 49 is 7 and that the closest square root of 66 is 8. For the computer program to "know" all of the square roots of two digit numbers, a 100 element table will have to be devised that contains the square roots. Alternatively, the guessing game can be used to determine these values.)
As it turns out, determining square roots is easier if you're dealing with a binary number system. No guesswork, just simple mathematics. This is easy to see when you're dealing with a number that is a power of two (2, 4, 8, etc...) Simply right shift the number halfway to the right. (Shift bit-0 0 bits, bit-1 0 bits, bit-2 1 bit, bit-3 1 bit, bit-4, 2 bits, bit-5 2 bits, etc.) Repeated application of this rule will allow you determine the square root of values that are not powers of 2.
First, let's look at a couple of perfect squares.
49 = 7*7. The binary representation is 110001 = 111 * 111.
Working forward, 7*7 in binary is 111 * 111. Performing all of the cross multiplication yields:
49 = 4*(4 + 2 + 1) + 2*(4 + 2 + 1) + 1*(4 + 2 + 1) which further expands to:
49 = (4*4 + 4*2 + 4*1) + (2*4 + 2*2 + 2*1) + (1*4 + 1*2 + 1*1)
Similarly, 144 = 12*12. The binary representation is 10010000 = 1100 * 1100.
Again working forward, 12*12 in binary is 1100 * 1100 and performing all of the cross multiplications yields:
144 = 8*(8 + 4) + 4*(8 + 4) which expands to:
144 = (8*8 + 8*4) + (4*8 + 4*4)
Examine both of these examples carefully. You'll note that some of the terms are squares (n*n) and some are not (n*m). The sum of the values in each term that is a square yields the square root.
49 = (4*4 + 4*2 + 4*1) + (2*4 + 2*2 + 2*1) + (1*4 + 1*2 + 1*1)
4*4 + 2*2 + 1*1
7 = 4 + 2 + 1
144 = (8*8 + 8*4) + (4*8 + 4*4)
8*8 + 4*4
12 = 8 + 4
This foundation is the basis for determing the square root of values that are not perfect squares.
Consider the value 156. It's closest square root is 12. We can derive the integral square root by using what we learned above.
156 = 10011100 = 128 + 16 + 8 + 4
128 >> 4 + 16 >> 2 + 8 >> 2 + 4 >> 1
8 + 4 + 2 + 2
Clearly we're looking for a number between 12 and 13 so adding all of these values yeilds a number (16) that just can't be. What's happened is that the decimal point should occur between the second and third digit. By adding from left to right, we simply don't add any value that causes the sum to be larger than the integral square root.
The mathematics for determining the fractional portion of the square root is a bit more complicated, but very manageable. The algorithm supplied by jobrienct appears to calculate the integral portion of a square root using the mathematics that I've described here.
Good Luck,
Kent
well if the author didnt enjoy reading that, i sure did :)
The code I posted does exactly that, and is optimized to do it quickly on a range of cpu's. I didn't write it, I just found and posted it as a possible solution. I chose it from among several because I prefer it's method to the guessing game, which is nearly always slower.
John
Another interesting way for integer square roots
Add up all the odd numbers from 1 while the sum is less than or equal to the number whose square root is being sought. The number of odd numbers needed is the square root.
For example, the integer square root of 23 = 4
1 + 3 + 5 + 7 = 16
1 + 3 + 5 + 7 + 9 = 25
Fast integer sqrt
long sqrt(r)
long r;
{
long t,b,c=0;
for (b=0x10000000;b!=0;b>>=2) {
t = c + b;
c >>= 1;
if (t <= r) {
r -= t;
c += b;
}
}
return(c);
}
But most of the times, you would be dealing with floats ... you can use one of the numerical analysis approaches (e.g. Newton's method), but they all take up some guessing
http://www.geocities.com/S
Some more methods
http://mathworld.wolfram.c
Hi Kdo,
I'm not sure about the mathematical proof behind it but try it with a program:
for (int i = 0; i < 100; i++)
{
if (pow (i, 0.5) == sqrt(i))
printf ("Checks out! \n");
else
printf ("Does not check out \n");
}
I think that's right.. I'd never actually use a program like this for practical uses though.
see ya
Hi jkr,
I'm positive that this is homework. My response was intended to "teach", not "do". :)
Hi dmatter,
pow() may be a single API in a C program, but the object code isn't nearly so simple. Using pow() might solve the original poster's requirements, but the mathematics involved is at least as complicated as anything posted here. By the way, depending on how pow() and sqrt() have been implemented your test may fail. Floating point math (especially division) is an imprecise science on Intel chips and the least significant bits often stray so pow() and sqrt() might return slightly different answers.
Hi jobrienct,
Glad that you enjoyed reading it. I did enjoy typing it up and actually getting to think a little bit. It's pretty sad that the world that Micro$oft has steered us into has removed the need for most of us to think. A while back I had to write a checksum algorithm for a partner company when they discovered that their algorithm was full of holes. A multi-million dollar company in the mathematics business and they had to rely on "an amateur" to do the math for them. Sad.....
Kent
Kdo,
Hmm, I never considered that point. Perhaps a rounding routine could be used to elimate the LSBs (least sig bits) that may differ, although I'm pitching in the dark here - I'd have to look into it more but it doesn't rank particularly high on my agenda (Deadlines for other work are drawing near) :?
Dave
All,
I've been programming for >20 years and I've never come across either of these really cute algorithms for sqrt (i.e. adding up odds and folding alternate bits). Is there a source of other cute stuff like this.
P.S. A small challenge, what does ( n ^ (n-1) ) + 1 achieve and can anyone remember ever using this in anger? (I mean '^' as in 'xor', not pow ;) ).
Paul
Good call, Sunny.
n^(n-1) is a "lobit()" function that sets all of the bits to the left of the least significant 1 bit to zero and all of the bits to the right of it to 1, creating a mask in the low end of the word. Adding 1 simply "rounds up" to the next power of 2.
N (N ^ (N-1)) + 1
7 (111 ^ 110) + 1
001 + 1 = 010
14 (1110 ^ 1101) + 1
0011 + 1 = 0100
etc...
Business Accounts
Answer for Membership
by: ashu1004Posted on 2004-01-01 at 23:50:45ID: 10027905
\\\