div
func main() {
{
a := 100;
b := 6;
q := 0;
r := a;
for ;r >= b; {
r -= b;
q++;
}
fmt.Println(q)
fmt.Println(r)
}
d := new(big.Int).SetInt64(100)
d.Lsh(d, 64*3)
e := new(big.Int).SetInt64(6)
e.Lsh(e, 64)
e.Add(e, new(big.Int).SetUint64((uint64(1) << 63) - 1 + (uint64(1) << 63)))
e.Lsh(e, 64)
e.Add(e, new(big.Int).SetUint64((uint64(1) << 63) - 1 + (uint64(1) << 63)))
s := new(big.Int).Div(d, e)
fmt.Println(s)
}
https://wikimili.com/en/Euclidean_division
The following proof of the division theorem relies on the fact that a decreasing sequence of non-negative integers stops eventually. It is separated into two parts: one for existence and another for uniqueness of and . Other proofs use the well-ordering principle (i.e., the assertion that every non-empty set of non-negative integers has a smallest element) to make the reasoning simpler, but have the disadvantage of not providing directly an algorithm for solving the division (see § Effectiveness for more). [7]
Existence
Consider first the case b < 0. Setting b' = –b and q' = –q, the equation a = bq + r may be rewritten as a = b'q' + r and the inequality 0 ≤ r < |b| may be rewritten as 0 ≤ r < |b′|. This reduces the existence for the case b < 0 to that of the case b > 0.
Similarly, if a < 0 and b > 0, setting a' = –a, q' = –q – 1, and r' = b – r, the equation a = bq + r may be rewritten as a' = bq' + r′, and the inequality 0 ≤ r < |b| may be rewritten as 0 ≤ r' < |b|. Thus the proof of the existence is reduced to the case a ≥ 0 and b > 0 — which will be considered in the remainder of the proof.
Let q1 = 0 and r1 = a, then these are non-negative numbers such that a = bq1 + r1. If r1 < b then the division is complete, so suppose r1 ≥ b. Then defining q2 = q1 + 1 and r2 = r1 – b, one has a = bq2 + r2 with 0 ≤ r2 < r1. As there are only r1 non-negative integers less than r1, one only needs to repeat this process at most r1 times to reach the final quotient and the remainder. That is, there exist a natural number k ≤ r1 such that a = bqk + rk and 0 ≤ rk < |b|.
This proves the existence and also gives a simple division algorithm for computing the quotient and the remainder. However, this algorithm is not efficient, since its number of steps is of the order of a/b.
Uniqueness
The pair of integers r and q such that a = bq + r is unique, in the sense that there can't be other pair of integers that satisfies the same condition in the Euclidean division theorem. In other words, if we have another division of a by b, say a = bq' + r' with 0 ≤ r' < |b|, then we must have that
- q' = q and r' = r.
To prove this statement, we first start with the assumptions that
- 0 ≤ r < |b|
- 0 ≤ r' < |b|
- a = bq + r
- a = bq' + r'
Subtracting the two equations yields
- b(q – q′) = r′ – r.
So b is a divisor of r′ – r. As
- |r′ – r| < |b|
by the above inequalities, one gets
- r′ – r = 0,
and
- b(q – q′) = 0.
Since b ≠ 0, we get that r = r′ and q = q′, which proves the uniqueness part of the Euclidean division theorem.
Effectiveness
In general, an existence proof does not provide an algorithm for computing the existing quotient and remainder, but the above proof does immediately provide an algorithm (see Division algorithm#Division by repeated subtraction), even though it is not a very efficient one as it requires as many steps as the size of the quotient. This is related to the fact that it uses only additions, subtractions and comparisons of integers, without involving multiplication, nor any particular representation of the integers such as decimal notation.
In terms of decimal notation, long division provides a much more efficient algorithm for solving Euclidean divisions. Its generalization to binary and hexadecimal notation provides further flexibility and possibility for computer implementation. [1] However, for large inputs, algorithms that reduce division to multiplication, such as Newton–Raphson, are usually preferred, because they only need a time which is proportional to the time of the multiplication needed to verify the result—independently of the multiplication algorithm which is used (for more, see Division algorithm#Fast division methods).