VOOZH about

URL: https://www.geeksforgeeks.org/dsa/vantieghems-theorem-primality-test/

⇱ Vantieghems Theorem for Primality Test - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vantieghems Theorem for Primality Test

Last Updated : 23 Aug, 2022

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 (21 - 1) * (22 - 1) = 1*3 = 3. 3 is congruent to 3 mod 7. We get 3 mod 7 from expression 3 * (mod (23 - 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. 
 


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.

Comment
Article Tags: