Vantieghems定理是数成为质数的必要和充分条件。它指出,对于自然数n为素数, 在哪里 ,与 。
换句话说,当且仅当数字n为质数。
例子:
- 对于n = 3,最终乘积为(2 1 – 1)*(2 2 – 1)= 1 * 3 =3。3与3 mod 7一致。我们从表达式3 *(mod(2 3 – 1)),因此3是质数。
- 对于n = 5,最终乘积为1 * 3 * 7 * 15 =315。315与5(mod 31)相等,因此5为质数。
- 对于n = 7,最终乘积为1 * 3 * 7 * 15 * 31 * 63 =615195。615195与7(mod 127)一致,因此7为质数。
- 对于n = 4,最终乘积1 * 3 * 7 =21。21不等于4(mod 15),因此4是合成的。
高于定理的另一种陈述方式是分界 ,则n为质数。
C++
// C++ code to verify Vantieghem's Theorem
#include
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
// 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
# 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#
// 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.
Javascript
输出:
2 is prime
3 is prime
5 is prime
7 is prime
上面的代码不适用于大于11的n值。它会导致prod评估中的溢出。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。