c++ - C++ 中的威尔逊定理,输出不正确?

标签 c++ proof theorem

我编写了一个函数,该函数应使用 Wilson 定理检查整数是否为质数。但是它输出 5 不是质数,这显然是。我想请问这是为什么?

#include <iostream>
using namespace std;
long int counter = 1;
bool primeWilson(int n)
{
    for(int i=1; i<n; i++)
    {
        counter*=i;
    }
    if(n%(counter+1)!=0)
    {
        return true;
    }
    return false;
}

最佳答案

您正在检查实现 Wilson 的错误方法。 例如:

25能被5整除
但是
5 不能被 25 整除

n%(counter+1)

->

(counter+1)%n

关于c++ - C++ 中的威尔逊定理,输出不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48438284/

相关文章:

algorithm - 如何通过归纳法证明二分查找是正确的呢?

coq - 证明助手是如何实现的?

c++ - 窃取一个窗口到 CreateWindow 窗口创建一个 "frozen"窗口?

c++ - Cocos2d-x lua绑定(bind)

tic-tac-toe - 将计算函数的值用于agda中的证明

latex - 对于共享相同计数器的定理环境,cleveref 失败

algorithm - 柏姆-雅可比尼定理

c++ - 传递接受任意数量和类型参数的函数作为类模板参数

c++ - 从用户那里获取 c++ 中的字符串,并显示它