home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!comlab.ox.ac.uk!comlab.ox.ac.uk!mnewman
- From: mnewman@comlab.ox.ac.uk (Matthew Newman)
- Newsgroups: sci.math
- Subject: Square root alg.
- Message-ID: <1993Jan22.160539.5493@client41.comlab.ox.ac.uk>
- Date: 22 Jan 93 16:05:39 GMT
- Sender: mnewman@comlab.ox.ac.uk (Matthew Newman)
- Organization: Oxford University Computing Laboratory, UK
- Lines: 65
- Originator: mnewman@client41.comlab
-
- Someone asked for a square root algorithm.
-
- To work out what the algorithm is observe
- that if N = a1 + 100a2 (+ 10000a3 + ...)
- and Sqrt(N) = b1 + 10b2 (+ 100 b3 + ...)
- then N = (b1 + 10b2)(b1 + 10b2) = b1^2 + 100b2^2 + 20b1b2
-
- So, suppose you want to calculate the square root of N, we keep
- track of three numbers, Res (the result), Rem (a remainder type
- variable) and Add (something else!).
-
- From the decimal point, pair off the digits of N (both left and
- right e.g. N = 127.345 -> 01 27 . 34 50)
-
- Initially Res = 0
- Rem = Left most pair from N
- Add = 0
-
- Recursively choose x largest such that (Add + x)x <= Rem
-
- Next, let Res' = Res*10 + x
- Rem' = 100*(Rem-(Add + x)x) + next pair of digits from N
- Add' = 10*(Add + 2x)
-
- and repeat 'till done (i.e. until Rem=0 or Res is long enough). You have
- to keep track of where the decimal point comes into Res, but it's fairly
- obvious (!).
-
- An example might help !
-
- N = 4096 = 40 96
- Res = 0
- Rem = 40
- Add = 0
- So the first x is 6 (since 6*(0+6)=36, and 7*(0+7)=49)
- Res' = 6
- Rem' = 400 + 96 = 496
- Add = 120
- The next x is thus 4, since 4*(120+4)=496
- Res'' = 64
- Rem'' = 0
- Add = 256
- and we're done
-
- N = 2 = 02 . 00 00 00 00 ...
- Res = 0
- Rem = 2
- Add = 1
- so x is 1
- Res' = 1
- Rem' = 100
- Add' = 20
- so x is 4
- Res'' = 1.4 (note that the decimal point comes in here ...)
- Rem'' = 400
- Add'' = 280
- so x is 1
- Res''' = 1.41
- Rem''' = 11900
- Add''' = 2820
- so x is 4
- Res'''' = 1.414
- Rem'''' = 60400
- Add'''' = 28280
- and so on for ever ...
-