📜  莱曼素性检验

📅  最后修改于: 2021-09-22 10:53:44             🧑  作者: Mango

大于 1 的整数 p 是素数,当当 p 的唯一除数是 1 和 p。前几个素数是 2, 3, 5, 7, 11, 13, …

Lehmann 检验是对自然数 n 的概率素性检验,它可以检验任何一种数的素性(无论大奇数是否为素数)。莱曼检验是费马素性检验的变体。

使用的方法如下:
如果 ‘n’ 是一个奇数并且 ‘a’ 是一个小于 n 但大于 1 的随机整数,那么

x = (a^((n-1)/2)) (mod n)

它是计算出来的。

  1. 如果 x 是 1 或 -1(或 n-1),则 n 可能是素数。
  2. 如果 x 不是 1 或 -1(或 n-1),则 n 肯定是合数。

在这种情况下,任何合数都可以被证明是素数这一事实取决于随机值“a”。如果 a 和 n 的所有值互质,则 n 可以说是质数。

Example-1:
Input: n = 13 
Output: 13 is Prime 

Explanation: 
Let a = 3, then, 
3^((13-1)/2) % 13 = 729 % 13 = 1
Hence, 13 is Prime.

Example-2:
Input: n = 91
Output: 91 is Composite

Explanation: 
Let a = 3, then, 
3^((91-1)/2) % 91 = 27
Hence, 91 is Composite. 
C++
// C++ code for Lehmann's Primality Test
#include
#include
#include
#include
using namespace std;
  
// function to check Lehmann's test
int lehmann(int n, int t)
{
  
    // generating a random base less than n
    int a = 2 + (rand() % (n - 1));
  
    // calculating exponent
    int e = (n - 1) / 2;
  
    // iterate to check for different base values 
    // for given number of tries 't'
    while(t > 0)
    {
  
        // calculating final value using formula
        int result =((int)(pow(a, e)))% n;
  
        //if not equal, try for different base
        if((result % n) == 1 || (result % n) == (n - 1)) 
        {
            a = 2 + (rand() % (n - 1));
            t -= 1;
        }
  
        // else return negative
        else
            return -1;
    }
  
    // return positive after attempting
    return 1;
}
  
// Driver code
int main()
{
    int n = 13 ; // number to be tested
    int t = 10 ; // number of tries
  
    // if n is 2, it is prime
    if(n == 2)
    cout << "2 is Prime.";
  
    // if even, it is composite
    if(n % 2 == 0)
        cout << n << " is Composite";
  
    // if odd, check
    else
    {
        int flag = lehmann(n, t);
      
        if(flag ==1)
            cout << n << " may be Prime.";
      
        else
            cout << n << " is Composite.";
    } 
}
  
// This code is contibuted by chitranayal


Java
// Java code for Lehmann's Primality Test 
      
// importing "random" for random operations 
import java.util.Random;
  
class GFG
{
  
    // function to check Lehmann's test 
    static int lehmann(int n, int t)
    {
      
        // create instance of Random class 
        Random rand = new Random(); 
          
        // generating a random base less than n 
        int a = rand.nextInt(n - 3) + 2;
      
        // calculating exponent 
        float e = (n - 1) / 2;
      
        // iterate to check for different base values 
        // for given number of tries 't' 
        while(t > 0)
        {
      
            // calculating final value using formula 
            int result = ((int)(Math.pow(a, e))) % n;
      
            // if not equal, try for different base 
            if((result % n) == 1 || (result % n) == (n - 1))
            {
                a = rand.nextInt(n - 3) + 2;
                t -= 1;
            }
      
            // else return negative 
            else
                return -1;
                  
        }
          
        // return positive after attempting 
        return 1;
    }
      
    // Driver code 
    public static void main (String[] args)
    {
    int n = 13; // number to be tested 
    int t = 10; // number of tries 
      
    // if n is 2, it is prime 
    if(n == 2)
        System.out.println(" 2 is Prime."); 
      
    // if even, it is composite 
    if(n % 2 == 0)
        System.out.println(n + " is Composite");
      
    // if odd, check 
    else
    {
        long flag = lehmann(n, t);
      
        if(flag == 1)
            System.out.println(n + " may be Prime.");
      
        else
            System.out.println(n + " is Composite."); 
    }
}
}
  
// This code is contributed by AnkitRai01


Python3
# Python code for Lehmann's Primality Test
  
# importing "random" for random operations
import random
  
# function to check Lehmann's test
def lehmann(n, t):
  
    # generating a random base less than n
    a = random.randint(2, n-1)
  
    # calculating exponent
    e =(n-1)/2
  
    # iterate to check for different base values 
    # for given number of tries 't'
    while(t>0):
  
        # calculating final value using formula
        result =((int)(a**e))% n
  
        # if not equal, try for different base
        if((result % n)== 1 or (result % n)==(n-1)):
            a = random.randint(2, n-1)
            t-= 1
  
        # else return negative
        else:
            return -1
  
    # return positive after attempting
    return 1
  
# Driver code
n = 13    # number to be tested
t = 10    # number of tries
  
# if n is 2, it is prime
if(n is 2):
    print("2 is Prime.")
  
# if even, it is composite
if(n % 2 == 0):
    print(n, "is Composite")
  
# if odd, check
else:
    flag = lehmann(n, t)
  
    if(flag is 1):
        print(n, "may be Prime.")
  
    else:
        print(n, "is Composite.")


C#
// C# code for Lehmann's Primality Test 
using System;
  
class GFG
{
  
// function to check Lehmann's test 
static int lehmann(int n, int t)
{
  
    // create instance of Random class 
    Random rand = new Random(); 
      
    // generating a random base less than n 
    int a = rand.Next(n - 3) + 2;
  
    // calculating exponent 
    float e = (n - 1) / 2;
  
    // iterate to check for different base values 
    // for given number of tries 't' 
    while(t > 0)
    {
  
        // calculating final value using formula 
        int result = ((int)(Math.Pow(a, e))) % n;
  
        // if not equal, try for different base 
        if((result % n) == 1 || 
           (result % n) == (n - 1))
        {
            a = rand.Next(n - 3) + 2;
            t -= 1;
        }
  
        // else return negative 
        else
            return -1;
              
    }
      
    // return positive after attempting 
    return 1;
}
  
// Driver code 
public static void Main (String[] args)
{
    int n = 13; // number to be tested 
    int t = 10; // number of tries 
      
    // if n is 2, it is prime 
    if(n == 2)
        Console.WriteLine(" 2 is Prime."); 
      
    // if even, it is composite 
    if(n % 2 == 0)
        Console.WriteLine(n + " is Composite");
      
    // if odd, check 
    else
    {
        long flag = lehmann(n, t);
      
        if(flag == 1)
            Console.WriteLine(n + " may be Prime.");
      
        else
            Console.WriteLine(n + " is Composite."); 
    }
}
}
  
// This code is contributed by Rajput-Ji


输出:
13 may be Prime.