gpt4 book ai didi

java - 莱曼素性检验

转载 作者:行者123 更新时间:2023-12-03 21:41:46 25 4
gpt4 key购买 nike

<分区>

我正在做雷曼测试,下面的函数没有给出 100% 准确的输出。它说所有 p 都是质数。我一直在搜索,算法似乎是正确的。请问是什么问题?

private static boolean lehmanTest(int p, int tries) {

boolean isPrime = true;

int a = randomGenerator();

int e = (p - 1 )/2;
int result = (a^e) % p;

System.out.println ("Result: " + result);

while (tries!=0)
{
if(result % p != 1 && result % p != p - 1)
{
a = randomGenerator();
tries--;
}
else
{
isPrime = false;
}
}
return isPrime;

修改后的代码

private static boolean lehmanTest(int p, int tries) {

//boolean isPrime = true;

//generate random number a
int a = randomGenerator(p);


int e = (p - 1 )/2;
int result = ((int)Math.pow(a,e)) % p;


while (tries!=0)
{
result = ((int)Math.pow(a,e)) % p;


if(result % p != p - 1)
//if(result % p != 1 && result % p != p - 1)
{
a = randomGenerator(p);
tries--;
}
else
{
return false;
}
}
return true;

}
private static int randomGenerator (int p) {
//generate random numbers a, n times
Random rand = new Random();

int randomInt = rand.nextInt(p);
return randomInt;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com