Advertisement

01.01.2004 at 11:48PM PST, ID: 20838828
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

to find square root of a number
Tags: square, root, find
how to find square root of a number without using sqrt function
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: ashu1004
Solution Provided By: jobrienct
Participating Experts: 8
Solution Grade: A
Views: 433
Translate:
Loading Advertisement...
01.01.2004 at 11:50PM PST, ID: 10027905

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.01.2004 at 11:56PM PST, ID: 10027918

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2004 at 12:00AM PST, ID: 10027925

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2004 at 12:38AM PST, ID: 10027981

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.02.2004 at 12:08PM PST, ID: 10030567

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.03.2004 at 01:16PM PST, ID: 10034992

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.03.2004 at 03:25PM PST, ID: 10035417

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.03.2004 at 11:24PM PST, ID: 10036473

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 03:01AM PST, ID: 10036760

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 03:15AM PST, ID: 10036786

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 05:49AM PST, ID: 10037054

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 10:16AM PST, ID: 10037890

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 10:28AM PST, ID: 10037949

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 11:07AM PST, ID: 10038106

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 01:32PM PST, ID: 10038593

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.04.2004 at 05:11PM PST, ID: 10039342

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.05.2004 at 12:23PM PST, ID: 10046662

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2004 at 03:55AM PST, ID: 10051414

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2004 at 04:41AM PST, ID: 10051671

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2004 at 04:57AM PST, ID: 10051760

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2004 at 05:00AM PST, ID: 10051776

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2004 at 05:03AM PST, ID: 10051791

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2004 at 05:09AM PST, ID: 10051813

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.31.2004 at 12:41AM PST, ID: 10241403

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.07.2004 at 02:20PM PST, ID: 10300337

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.01.2004 at 11:50PM PST, ID: 10027905
\\\
 
01.02.2004 at 12:00AM PST, ID: 10027925
static unsigned mborg_isqrt3(unsigned long val)
{
  unsigned long temp, g=0, b = 0x8000, bshft = 15;
  do {
    if (val >= (temp = (((g<<1)+b)<<bshft--))) {
      g += b;
      val -= temp;
    }
  } while (b >>= 1);
  return g;
}

John
Accepted Solution
 
01.02.2004 at 12:38AM PST, ID: 10027981
 
01.02.2004 at 12:08PM PST, ID: 10030567

Rank: Sage

Hm, if this is not homework,

double dSquareRoot = pow ( <any number>, 0.5);

should do it :o)
 
01.03.2004 at 01:16PM PST, ID: 10034992

Rank: Sage

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
Assisted Solution
 
01.03.2004 at 03:25PM PST, ID: 10035417
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
 
01.03.2004 at 11:24PM PST, ID: 10036473

Rank: Sage

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/SiliconValley/Garage/3323/aat/a_sqrt.html

Some more methods
http://mathworld.wolfram.com/SquareRootAlgorithms.html
Assisted Solution
 
01.04.2004 at 03:01AM PST, ID: 10036760
It's far simpler than any of this...

The squareroot of any number (n) is equivalent to that number with the power of a half:

sqrt(n) == n^0.5

Hope that helps

:]
 
01.04.2004 at 03:15AM PST, ID: 10036786

Rank: Wizard

But in C, ^ means xor
 
01.04.2004 at 05:49AM PST, ID: 10037054

Rank: Sage

Hi dmatter,

Care to demonstrate how n raised to the 1/2 power is simpler than anything else mentioned here?

Kent
 
01.04.2004 at 10:16AM PST, ID: 10037890
Anyone else get the feeling we may have aided and abetted in homework here? <g>

John
 
01.04.2004 at 10:28AM PST, ID: 10037949

Rank: Sage

>>Anyone else get the feeling we may have aided and abetted in homework here?

Check my comment :o)
 
01.04.2004 at 11:07AM PST, ID: 10038106
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
 
01.04.2004 at 01:32PM PST, ID: 10038593

Rank: Sage

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
 
01.04.2004 at 05:11PM PST, ID: 10039342

Rank: Sage

>>Hi jkr,
>>I'm positive that this is homework.  My response was intended to "teach", not "do".  :)

Well, that I know, and you are right, appropriate and correct in your answer :o)
 
01.05.2004 at 12:23PM PST, ID: 10046662
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
 
01.06.2004 at 03:55AM PST, ID: 10051414

Rank: Guru

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
 
01.06.2004 at 04:41AM PST, ID: 10051671

Rank: Sage

gives double the value of least significant 1 in the number ?
 
01.06.2004 at 04:57AM PST, ID: 10051760

Rank: Guru

Close. Its the least significant bit of a binary value.

E.G. ( 10110001000000 ^ (10110001000000 - 1) ) + 1
-> ( 10110001000000 ^ 10110000111111 ) + 1
-> 111111 + 1
-> 1000000

Anyone ever used it?
 
01.06.2004 at 05:00AM PST, ID: 10051776

Rank: Sage


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...


 
01.06.2004 at 05:03AM PST, ID: 10051791

Rank: Sage

Hi Paul,

You missed a bit:

E.G. ( 10110001000000 ^ (10110001000000 - 1) ) + 1
-> ( 10110001000000 ^ 10110000111111 ) + 1
>> 1111111 + 1
>> 10000000


Kent
 
01.06.2004 at 05:09AM PST, ID: 10051813

Rank: Sage

Thanks Kent :o)
 
01.31.2004 at 12:41AM PST, ID: 10241403

Rank: Sage

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: jobrienct {http:#10027925} & Kdo {http:#10034992} & sunnycoder {http:#10036473}

Please leave any comments here within the next seven days.

Sunnycoder
EE Cleanup Volunteer
 
02.07.2004 at 02:20PM PST, ID: 10300337
Forced accept

Computer101
E-E Admin
 
 
20080236-EE-VQP-29