Wilson Theorem in C++, incorrect output?

203 views Asked by At

I have written a function that should check whether an integer is prime, using the Wilson theorem. However it outputs that 5 is not a prime number, which obviously is. I want to ask why is that?

#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;
}
1

There are 1 answers

0
Yunnosch On

You are checking the wrong way around for implementing Wilson. For example:

25 is divisable by 5
but
5 is not divisable by 25

n%(counter+1)

->

(counter+1)%n