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).
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理