-1
我想知道如果A和B是相对使用欧几里德算法的素数。 A和B是不能以任何数据类型(C语言)存储的大数字,因此它们存储在链接列表中。在该算法中,使用运营商%
。我的问题是,有没有一种方法可以计算A mod B,而不需要直接使用%
运算符。我发现%
是分配了另外:A模B,A和B是非常大的数字
A%B = ((a1%B)+(a2%B))%B.
但问题仍然存在,因为我仍然会做%B
操作。
我想知道如果A和B是相对使用欧几里德算法的素数。 A和B是不能以任何数据类型(C语言)存储的大数字,因此它们存储在链接列表中。在该算法中,使用运营商%
。我的问题是,有没有一种方法可以计算A mod B,而不需要直接使用%
运算符。我发现%
是分配了另外:A模B,A和B是非常大的数字
A%B = ((a1%B)+(a2%B))%B.
但问题仍然存在,因为我仍然会做%B
操作。
如果没有%
运算符,则需要计算a % b
。好?根据定义,modulo operation在将一个数字除以另一个数字后找到remainder。
在蟒蛇:
# mod = a % b
def mod(a, b):
return a-b*int(a/b)
>>> x = [mod(i,j) for j in range(1,100) for i in range(1,100)]
>>> y = [i % j for j in range(1,100) for i in range(1,100)]
>>> x == y
真
在C++:
#include <iostream>
#include <math.h>
using namespace std;
unsigned int mod(unsigned int a, unsigned int b) {
return (unsigned int)(a-b*floor(a/b));
}
int main() {
for (unsigned int i=1; i<=sizeof(unsigned int); ++i)
for (unsigned int j=1; j<=sizeof(unsigned int); ++j)
if (mod(i,j) != i%j)
cout << "Somthing wrong!!";
cout << "Proved for all unsigned int!";
return 0;
}
事实证明,所有的unsigned int类型!
现在,只需将结果扩展到您的大数字...... !!!
你想要[二进制GCD算法](https://en.wikipedia.org/wiki/Binary_GCD_algorithm) –