谁能解释一下模块化算术在编程中的工作原理?我知道它用于对大值进行操作。
例如,使用 int 数据类型计算 B(1000000, 2) 的二项式系数。我假设我们不能使用 int 数据类型进行乘法运算,因为它涉及计算像 1000000 这样的大值的阶乘!它有数百万位数字,不适合 32 位或 64 位整数。
我知道模运算用于这类问题,但我不明白它是如何工作的。
谁能解释一下模块化算术在编程中的工作原理?我知道它用于对大值进行操作。
例如,使用 int 数据类型计算 B(1000000, 2) 的二项式系数。我假设我们不能使用 int 数据类型进行乘法运算,因为它涉及计算像 1000000 这样的大值的阶乘!它有数百万位数字,不适合 32 位或 64 位整数。
我知道模运算用于这类问题,但我不明白它是如何工作的。
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2
as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0
means the number is even.
For more information please check Wikipedia.