Method of implementing integer open square algorithm using SHIFT, addition and subtraction (turn)

Source: Internet
Author: User

Method of implementing integer open square algorithm using SHIFT, addition and subtraction (turn)

This algorithm only uses shift, addition and subtraction, judgment and loop implementation, because it does not need floating-point operation, and does not need multiplication operation, so it can be easily applied to a variety of chips up.

Let's take a look at how the 10 binary is calculated by hand.
First look at the following two formulas,
x = 10*p + q (1)
The formula (1) is left and right after the square:
x^2 = 100*p^2 + 20PQ + q^2 (2)
Now suppose we know x^2 and P, hope to find out Q, find out the Q also find out the root of the x^2 x.
We rewrite the formula (2) to the following format:
Q = (x^2-100*p^2)/(20*P+Q) (3)

This equation has q, so it is not possible to directly calculate the Q, so the manual algorithm and manual Division algorithm as a step need to guess the value.

Let's take a hand-calculated example: Calculate 1234567890 of the root

First, we separate the number two bits and two bits, and calculate the maximum of 3. That is, p in (3), and 334 of the bottom row is the remainder, which is the approximate value (x^2-100*p^2) in the formula (3)
3
---------------
/12 34 56 78 90
9
---------------
/3 34

Below we are going to find a 0-9 number q so that it is closest to satisfying the formula (3). Let's just multiply p by 20 and write on 334 left:
                            3  Q
                           ---------------
                        /The
                             9
                          ---------------
(20*3+q ) *q      /  3

We see that the value of Q for 5 o'clock (60+Q) *q is closest to 334 and not more than 334. So we get:
3 5
---------------
/12 34 56 78 90
9
---------------
65/3 34
3 25
---------------
9 56

The next step is to repeat the above steps, and here is no longer verbose.

This manual algorithm actually has little to do with the 10 binary, so we can easily change it to binary, then the formula (3) becomes:
Q = (x^2-4*p^2)/(4*P+Q) (4)

Let's look at an example to calculate the root of 100 (binary 1100100):
1 0 1 0
-----------
/1 10 01 00
1
-----------
100/0 10
0 00
-----------
1001/10 01
10 01
-----------
0 00

Here each step is no longer the P multiplied by 20, but the p is multiplied by 4, that is, the p to the right two bits, and because the value of Q can only be 0 or 1, so we only need to determine the remainder (x^2-4*p^2) and (4*p+1) size relationship, if the remainder is greater than or equal to (4*P+Q) then the previous 1, Otherwise the previous 0.

The complete C language program is given below, where Root indicates that P,rem represents the remainder after each step of the calculation, divisor (4*p+1), by A>>30 to the highest 2 bits of a, and by a<<=2 the highest calculated 2 bits are removed. The root of which two times <<1 is equivalent to 4*p. The program is completely rewritten according to manual calculation, it should not be difficult to understand.
unsigned short sqrt (unsigned long a) {
unsigned long rem = 0;
unsigned long root = 0;
unsigned long divisor = 0;
for (int i=0; i<16; ++i) {
Root <<= 1;
rem = ((rem << 2) + (a >> 30));
A <<= 2;
Divisor = (root<<1) + 1;
if (divisor <= rem) {
REM-= divisor;
root++;
}
}
return (unsigned short) (root);
}

Posted on 2008-01-23 14:21 QUIRE-0216 Read (2127) Comments (1) Edit collection reference belongs to Category: Arithmetic (algorithm)

feedback# Re: The method of implementing integer open square algorithm using SHIFT, addition and subtraction (turn) 2008-01-23 14:46 QUIRE-0216 for everyone to understand I finished the above 1234567890!
3 5 Q
---------------
/12 34 56 78 90
9
---------------
65/3 34
3 25
---------------
(20*35+q) *Q/9 56
We see that the value of Q for 1 o'clock (700+Q) *q is closest to 956 and not more than 956. So we get:
3 5 1 Q
---------------
/12 34 56 78 90
9
---------------
65/3 34
3 25
---------------
701/9 56
7 01
----------------
(20*351+Q) *Q/2 55 78

We see that the value of Q for 3 o'clock (20*351+Q) *q is closest to 25578 and not more than 25578. So we get:

3 5 1 3 q
---------------
/12 34 56 78 90
9
---------------
65/3 34
3 25
---------------
701/9 56
7 01
----------------
7023/2 55 78
2 10 69
----------------
(20*3513+q) *q/45 0990

We see that the value of Q for 6 o'clock (20*3513+Q) *q is closest to 450990 and not more than 450990. So we get:
3 5 1) 3 6
---------------
/12 34 56 78 90
9
---------------
65/3 34
3 25
---------------
701/9 56
7 01
----------------
7023/2 55 78
2 10 69
----------------
70266/45 0990
42 1596
----------------
2 9394

The root of this 1234567890 is 35136. I think I can see it!

Method of implementing integer open square algorithm using SHIFT, addition and subtraction (turn)

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.