我曾尝试为Fermat素性测试编写代码,但显然失败了.所以,如果我深知:如果p
是素数,那么((a^p)-a)%p=0
在那里p%a!=0
.我的代码似乎没问题,因此我很可能误解了基础知识.我在这里错过了什么?
private bool IsPrime(int candidate)
{
//checking if candidate = 0 || 1 || 2
int a = candidate + 1; //candidate can't be divisor of candidate+1
if ((Math.Pow(a, candidate) - a) % candidate == 0) return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)