C language implementation of square root (ii.)--principle of square root of hand

Source: Internet
Author: User
Tags bitwise square root tidy

A function mathematically can have an infinite number of function columns converge to this function, then the program approximation implementation can have countless kinds of algorithms, square root nature is no exception.

Do not know how many people still remember the square root of the hand, it is to satisfy each time in the result of adding a bit, that is, the bitwise approximation of the results of the only algorithm. As for how to prove this uniqueness mathematically, I will not say that mathematics proves that not so many people are interested. The bitwise approximation is more suitable for hand calculation, and as a more familiar example, hand division. I'm going to use a bitwise approximation of the hand method here.

To say the square root of the hand, the principle is actually very simple,

The first is that the square root function is a strictly monotonically increasing function,

The second is that the following identity satisfies

(a*n+b) 2≡ (a*n) 2 + 2*a*b*n + b2

≡ (a*n) 2 + b * ((a*n) * 2 + B)

Our example operates a square root written calculation, to explain.

Let's ask for the square root of 5499025.

First, the 54,990,252-bit two-bit from low to high row, for

5 49 90 25

2*2<5<3*3

So the highest bit is 2,

And then we'll look at the square root of 549,

We assume that the integer part of the square root of 549 is 2*10+b, then according to the previous identity, N here equals 10,a here equals 2, there is

549 >= (2*10) 2 + b * ((2*10) * 2 + B)

Tidy up, 149 >= b * (+ b)

3 * < 149 < 4 * 44

So b=3,

The square root integer portion of 549 is 23,

Then assume that the square root integer portion of 54990 is 23*10+b,

The

54990 >= (23*10) 2 + b * ((23*10) * 2 + B)

Tidy up, 2090 >= b * (460 + b),

464 * 4 < 2090 < 465 * 5

So b=4,

The square root integral part of 54990 is divided into 234,

Finally, we look at the integer portion of the square root of 5499025, assuming 234 * + B,

The

5499025 >= (234*10) 2 + b * ((234*10) * 2 + B)

Tidy up, 23425 >= b * (4680 + b)

and 5 * 4685 = 23425, the equation is set,

So ultimately we want the square root to be 2345. Of course, the decimal place can still be counted in this way.

The square root of the hand is the same as above from the high step to the status of the process, written in the form of a formula is generally as follows:

2 3 4 5

-------------------

| 5 49 90 25

2 | 4

-------------------

| 1 49

43 | 1 29--currently calculates 2,2*10*2 = 40

-------------------

| 20 90

464 | 18 56--currently calculates 23,23*10*2 = 460

-------------------

| 2 34 25

4685 | 2 34 25--currently calculates 234,234*10*2 = 4680

-------------------

0

Of course, how to write does not matter, know the process can continue our design. And then we're going to use this algorithm, to make a modification, to do binary opening squared.

Each bit of binary is not 1 or 0, so it is relatively simple to push forward one at a time.

For example, let's calculate the square root of 121, which is the square root of the binary 1111001.

Two-bit two-bit from low to high row

1 0 1 1

------------------

| 1 11 10 01

1 | 1

------------------

| 11

100 | 0--currently calculates the right movement of two bits to 100

------------------

| 11 10

1001 | 10 01--currently calculates the 10,1 right to move two bits to 1000

------------------

| 1 01 01

10101 | 1 01 01--currently calculates the 101,1 right move two bit to 10100

------------------

0

Each to the right to push 1 bits, the following divisor is the number of the current calculated binary right shift two digits plus 1 or plus 0

After that, we can use the square root of this algorithm to build.

C language implementation of square root (ii.)--principle of square root of hand

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.