- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Vantieghems Theorem for Primality Test
The problem statement includes using Vantieghems theorem for primality test i.e. we will check for a positive number N which will be user input and print if the number is a prime number or not using the Vantieghems theorem.
Vantieghem’s Theorem
The Vantieghems theorem for primality states that a positive number, N is a prime number if the product of $\mathrm{2^{i}−1}$ where the value of i ranges from 1 to N−1 is congruent to N modulo $\mathrm{2^{N}−1}$
If both the values are congruent then the number N is a prime number else it is not a prime number.
Congruent Numbers: The two numbers are said to be congruent modulo n if the difference of the two given numbers is perfectly divided by n or can say n is one of the factors of the difference of two numbers. According to the theorem, the two numbers will be the congruent numbers if $\mathrm{2^{N}−1}$ is one of the factors of the difference of product of $\mathrm{2^{i}−1}$ N i.e. it divides the difference with 0 remainder.
Let’s understand the theorem with some examples.
Input
N=3
Output
Yes
Explanation −The product of $\mathrm{2^{i}−1}$ where i ranges from 1 to N−1 i.e. 1<=i<=N−1 which will be $\mathrm{(2^{1}−1)*(2^{2}−1)=1*3=3}$
The value of N is 3 so for these numbers to be congruent the difference of the two numbers which is 0 must be divided by $\mathrm{(2^{N}−1)}$
Since the condition of the Vantieghems Theorem satisfies in this case, 3 is a prime number.
Input
N=5
Output
Yes
Explanation−The product of $\mathrm{(2^{i}−1)}$ where i ranges from 1 to 4 in this case since N=5.
$\mathrm{(2^{1}−1)*(2^{2}−1)*(2^{3}−1)*(2^{4}−1)=1*3*7*15=315}$
Here the value of N is 5 so for 315 and 5 to be congruent mod $\mathrm{(2^{N}−1)}$ i.e. 31, the difference of two numbers, 315 and 5 must be divided by 31 with no remainder left.
The difference is 310 which is divided by 31 to give 10, hence 5 is a prime number.
Input
N=6
Output
No
Explanation −The product of $\mathrm{(2^{i}−1)}$ where 1<=i<=N−1. In this case for N=6, the i will range from 1 to 5. So the value of the product will be,
$\mathrm{(2^{1}−1)*(2^{2}−1)*(2^{3}−1)*(2^{4}−1)*(2^{5}−1)=1*3*7*15*31=9765}$
The value of N is 6. So for 9765 and 6 to be congruent mod $\mathrm{(2^{N}−1)\:i.e\:(2^{6}−1)=63}$ 63 should be one of the factors of the difference of product of $\mathrm{(2^{i}−1)}$ and N which is 9765−6=9759.
Since 63 is not the factor of 9759, therefore 6 is not a prime number.
We will use the conditions of the Vantieghems theorem for a number to be a prime number in order to check if the given number N is a prime number or not in C++.
Algorithm
Since we know that any positive number N will be prime only if the product of $\mathrm{2^{i}−1}$ where i ranges from 1 to N−1 and N itself should be congruent mod $\mathrm{2^{N}−1}$
These means the difference between the product of $\mathrm{2^{i}−1}$ and N should be the multiple of $\mathrm{2^{N}−1}$ or in other words we can say the difference should be divisible by $\mathrm{2^{N}−1}$
To check the condition for a number to be a prime number according to the Vantieghems theorem we will simply iterate from i=2 to i<=N−1 since for i=1 the answer will be 1. Now we will keep storing the product of $\mathrm{2^{i}−1}$ for each value of i until i=N−1 to get the value of the product.
For calculating the value of $\mathrm{2^{i}−1}$ ,we will use bitwise operator i.e. left shift 1 by the value of i because when we left shift any number n with i the resultant is equal to $\mathrm{n*2^{i}}$ .So in this case we will left shift 1 by i to get the value of $\mathrm{2^{i}}$
Once we get the value of product then we will check for the congruence of product of $\mathrm{2^{i}−1}$, and N mod $\mathrm{2^{N}−1}$.For the product of $\mathrm{2^{i}−1}$ and N to be congruent the difference of the two should be divisible by $\mathrm{2^{N}−1}$ therefore we will check if the difference mod $\mathrm{2^{N}−1}$ is equal to zero. If it is equal to zero, the number will be a prime number.
For calculating the value of$\mathrm{2^{N}−1}$ we will simply left shift 1 by N which will be equal to$\mathrm{1*2^{N}}$
We will be using this algorithm in our approach to check the number for prime numbers in C++.
Approach
The steps to follow for the implementation of the Vantieghem’s theorem in our approach :
For checking the given number, if it is a prime number or not we will create a function where we will check using the condition for a number to be a prime number according to Vantieghem’s theorem.
Then initialise a variable to store the product of $\mathrm{2^{i}−1}$ where 1<=i<=N−1(N is the given number). The variable should be of long long data type as product of $\mathrm{2^{i}−1}$ could be large in value.
Then we will iterate in a for loop from i=1 to i<N and calculate the product of $\mathrm{2^{i}−1}$ using left shift operator to calculate the value of $\mathrm{2^{i}}$
Once we get the product of $\mathrm{2^{i}−1}$ now we will check if the difference of $\mathrm{2^{i}−1}$ and N is divisible by $\mathrm{2^{N}−1}$ or not because the difference of two must be the multiple of $\mathrm{2^{N}−1}$ to be the prime number.
If the condition satisfies we will return the number as a prime number else we will return it as not a prime number.
Example
// C++ code to check if the number is a prime number or not using Vantieghem's Theorem #include <bits/stdc++.h> using namespace std; //function to check the condition of Vantieghem's Theorem for a number to be a prime bool check(int N) { if(N==1){ return false; } long long product = 1; //to store the product of 2^i-1 for (int i=1; i < N; i++) { //to find the product of 2^i-1, where 1<=i<=N-1 product = product*((1LL << i) - 1); } //to check the condition of Vantieghem's Theorem //the product of 2^i-1 - N should be the multiple of 2^N -1 if ((product-N) % ((1LL << N) - 1) == 0 ){ //using left shift operator to calculate 2^N return true; } else{ return false; } } int main() { int N; N=1; //calling the function if(check(N)==true){ cout<<N<<" is a prime number"<<endl; } else{ cout<<N<<" is not a prime number"<<endl; } return 0; }
Output
1 is not a prime number
Note : The code will give the right output for values of N from 1 to 11 because the overflow of value in long long data type occurs for calculating the value of $\mathrm{2^i−1}$ for N>11.
Time complexity− O(N) , because we iterate in a for loop for N times to calculate the product of 2^i−1.
Space complexity − O(1) , as no extra space is taken to solve the problem.
Conclusion
We discussed Vantieghem's theorem for checking if the number is a prime number or not in this article in detail. We use the condition for a number to be a prime number in accordance with Vantieghem's theorem in our code in C++ to check if the number is a prime or not.
I hope you understand Vantieghem's theorem and solved all your doubts regarding the theorem after reading this article.