使用Java查找原始根
我试图得到质数的第一个本原根:
private static int GetPrime(int factors[], int modul) {
for (int i = 2; i < modul; i++) {
int j = 0;
while (j < factors.length) {
int power = (modul - 1) / factors[j];
long g = (long) Math.pow(i, power);
long mod = g % modul;
if (mod == 1) {
j++;
break;
} else if (j + 1 == factors.length) {
return i;
}
j++;
}
}
return 0;
}
作为参数,我有素数因子和素数因子。我试图实现查找第一个素根的函数,对于某些数字它是有效的,但对于其他数字它失败了。例如,它给出了23的正确答案,即5,但对于71,它失败了,而不是7,它给出了3。我搞不懂怎么了。 基本上,公式是n^((modul-1)/factor)%modul,如果对于其中一个因子,每个n的提醒是1,那么n不是素根
共 (0) 个答案