Vantieghems Theorem is a necessary and sufficient condition for a number to be prime. It states that for a natural number n to be prime, the product of where , is congruent to .

In other words, a number n is prime if and only if.

**Examples: **

- For n = 3, final product is (2
^{1}– 1) * (2^{2}– 1) = 1*3 = 3. 3 is congruent to 3 mod 7. We get 3 mod 7 from expression 3 * (mod (2^{3}– 1)), therefore 3 is prime. - For n = 5, final product is 1*3*7*15 = 315. 315 is congruent to 5(mod 31), therefore 5 is prime.
- For n = 7, final product is 1*3*7*15*31*63 = 615195. 615195 is congruent to 7(mod 127), therefore 7 is prime.
- For n = 4, final product 1*3*7 = 21. 21 is not congruent to 4(mod 15), therefore 4 is composite.

Another way to state above theorem is, if divides , then n is prime.

## C++

`// C++ code to verify Vantieghem's Theorem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `checkVantieghemsTheorem(` `int` `limit) ` `{ ` ` ` `long` `long` `unsigned prod = 1; ` ` ` `for` `(` `long` `long` `unsigned n = 2; n < limit; n++) { ` ` ` ` ` `// Check if above condition is satisfied ` ` ` `if` `(((prod - n) % ((1LL << n) - 1)) == 0) ` ` ` `cout << n << ` `" is prime\n"` `; ` ` ` ` ` `// product of previous powers of 2 ` ` ` `prod *= ((1LL << n) - 1); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `checkVantieghemsTheorem(10); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to verify Vantieghem's Theorem ` `def` `checkVantieghemsTheorem(limit): ` ` ` ` ` `prod ` `=` `1` ` ` `for` `n ` `in` `range` `(` `2` `, limit): ` ` ` ` ` `# Check if above condition is satisfied ` ` ` `if` `n ` `=` `=` `2` `: ` ` ` `print` `(` `2` `, ` `"is prime"` `) ` ` ` `if` `(((prod ` `-` `n) ` `%` `((` `1` `<< n) ` `-` `1` `)) ` `=` `=` `0` `): ` ` ` `print` `(n, ` `"is prime"` `) ` ` ` ` ` `# Product of previous powers of 2 ` ` ` `prod ` `*` `=` `((` `1` `<< n) ` `-` `1` `) ` ` ` `# Driver code ` `checkVantieghemsTheorem(` `10` `) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

**Output:**

2 is prime 3 is prime 5 is prime 7 is prime

The above code does not work for values of n higher than 11. It causes overflow in prod evaluation.

## Recommended Posts:

- Primality Test | Set 1 (Introduction and School Method)
- Primality Test | Set 3 (Miller–Rabin)
- AKS Primality Test
- Primality Test | Set 5(Using Lucas-Lehmer Series)
- Implementation of Wilson Primality test
- Primality test for the sum of digits at odd places of a number
- Primality Test | Set 4 (Solovay-Strassen)
- Primality Test | Set 2 (Fermat Method)
- Lucas Primality Test
- Chinese Remainder Theorem | Set 1 (Introduction)
- Wilson's Theorem
- Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Using Chinese Remainder Theorem to Combine Modular equations
- Corollaries of Binomial Theorem
- Fermat's little theorem
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Midy's theorem

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.