Open In App

Vantieghems Theorem for Primality Test

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:  




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

// 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;
}

                    
// Java code to verify Vantieghem's Theorem
import java.util.*;
class GFG
{
 
static void checkVantieghemsTheorem(int limit)
{
    long prod = 1;
    for (long n = 2; n < limit; n++)
    {
 
        // Check if above condition is satisfied
        if (((prod - n < 0 ? 0 : prod - n) % ((1 << n) - 1)) == 0)
            System.out.print(n + " is prime\n");
 
        // product of previous powers of 2
        prod *= ((1 << n) - 1);
    }
}
 
// Driver code
public static void main(String []args)
{
    checkVantieghemsTheorem(10);
}
}
 
// This code is contributed by rutvik_56.

                    
# 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

                    
// C# code to verify Vantieghem's Theorem
using System;
class GFG
{
  static void checkVantieghemsTheorem(int limit)
  {
    long prod = 1;
    for (long n = 2; n < limit; n++)
    {
 
      // Check if above condition is satisfied
      if (((prod - n < 0 ? 0 : prod - n) % ((1 << (int)n) - 1)) == 0)
        Console.Write(n + " is prime\n");
 
      // product of previous powers of 2
      prod *= ((1 << (int)n) - 1);
    }
  }
 
  // Driver code
  public static void Main()
  {
    checkVantieghemsTheorem(10);
  }
}
 
// This code is contributed by pratham76.

                    
<script>
 
// Javascript code to verify Vantieghem's Theorem
 
function checkVantieghemsTheorem( limit)
{
    let prod = 1;
    for (let n = 2; n < limit; n++) {
 
        // Check if above condition is satisfied
        if (n == 2)
            document.write(2 + " is prime" + "</br>");
        if (((prod - n) % ((1 << n) - 1)) == 0)
            document.write( n + " is prime" + "</br>");
 
        // product of previous powers of 2
        prod *= ((1 << n) - 1);
    }
}
 
// Driver Code
checkVantieghemsTheorem(10);
 
// This code is contributed by jana_sayantan.
</script>

                    

Output: 
2 is prime
3 is prime
5 is prime
7 is prime

 

Time Complexity : O(limit)
Auxiliary Space: O(1)



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


Article Tags :