📜  检查数字是否为Fermat伪素数

📅  最后修改于: 2021-05-04 22:13:17             🧑  作者: Mango

给定数字N和基数A。任务是检查该数字是否为基准的Fermat伪素数。
如果基数为A,则将数字N称为Fermat伪素数

例子:

方法:方法是检查以下条件:

  • 检查A> 1。
  • 检查N是否为整数。
  • 检查N是否除以A N-1 – 1。

如果满足以上所有条件,则N是对A的fermat伪素数。

下面是上述方法的实现:

C++
// C++ program to check if N is Fermat pseudoprime
// to the base A or not
#include 
using namespace std;
  
// Function to check if the given number is composite
bool checkcomposite(int n)
{
    // Check if there is any divisor of n less than sqrt(n)
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0)
            return 1;
    }
    return 0;
}
  
// Effectively calculate (x^y) modulo mod
int power(int x, int y, int mod)
{
  
    // Initialize result
    int res = 1;
  
    while (y) {
  
        // If power is odd, then update the answer
        if (y & 1)
            res = (res * x) % mod;
  
        // Square the number and reduce
        // the power to its half
        y = y >> 1;
        x = (x * x) % mod;
    }
  
    // Return the result
    return res;
}
  
// Function to check for Fermat Pseudoprime
bool Check(int n, int a)
{
  
    // If it is composite and satisfy Fermat criterion
    if (a>1 && checkcomposite(n) && power(a, n - 1, n) == 1)
        return 1;
  
    // Else return 0
    return 0;
}
  
// Driver code
int main()
{
  
    int N = 645;
    int a = 2;
      
   //  Function call
    cout << Check(N, a);
  
    return 0;
}


Java
// Java program to check if N is Fermat pseudoprime 
// to the base A or not 
class GFG 
{
  
    // Function to check if 
    // the given number is composite 
    static boolean checkcomposite(int n) 
    {
        // Check if there is any divisor of n 
        // less than sqrt(n) 
        for (int i = 2; i <= Math.sqrt(n); i++) 
        {
            if (n % i == 0) 
            {
                return true;
            }
        }
        return false;
    }
  
    // Effectively calculate (x^y) modulo mod 
    static int power(int x, int y, int mod)
    {
  
        // Initialize result 
        int res = 1;
  
        while (y != 0) 
        {
  
            // If power is odd,
            // then update the answer 
            if ((y & 1) == 1) 
            {
                res = (res * x) % mod;
            }
  
            // Square the number and reduce 
            // the power to its half 
            y = y >> 1;
            x = (x * x) % mod;
        }
  
        // Return the result 
        return res;
    }
  
    // Function to check for Fermat Pseudoprime 
    static int Check(int n, int a) 
    {
  
        // If it is composite and 
        // satisfy Fermat criterion 
        if (a > 1 && checkcomposite(n)
                && power(a, n - 1, n) == 1) 
        {
            return 1;
        }
  
        // Else return 0 
        return 0;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int N = 645;
        int a = 2;
  
        // Function call 
        System.out.println(Check(N, a));
    }
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to check if N is Fermat pseudoprime
# to the base A or not
  
from math import sqrt
  
# Function to check if the given number is composite
def checkcomposite(n):
      
    # Check if there is any divisor of n less than sqrt(n)
    for i in range(2,int(sqrt(n))+1,1):
        if (n % i == 0):
            return 1
    return 0
  
# Effectively calculate (x^y) modulo mod
def power(x, y, mod):
    # Initialize result
    res = 1
  
    while (y):
        # If power is odd, then update the answer
        if (y & 1):
            res = (res * x) % mod
  
        # Square the number and reduce
        # the power to its half
        y = y >> 1
        x = (x * x) % mod
  
    # Return the result
    return res
  
# Function to check for Fermat Pseudoprime
def Check(n,a):
    # If it is composite and satisfy Fermat criterion
    if (a>1 and checkcomposite(n) and power(a, n - 1, n) == 1):
        return 1
  
    # Else return 0
    return 0
  
# Driver code
if __name__ == '__main__':
    N = 645
    a = 2
  
    # Function call
    print(Check(N, a))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to check if N is Fermat pseudoprime 
// to the base A or not 
using System;
  
class GFG
{
      
    // Function to check if 
    // the given number is composite 
    static bool checkcomposite(int n) 
    { 
        // Check if there is any divisor of n 
        // less than sqrt(n) 
        for (int i = 2; i <= Math.Sqrt(n); i++) 
        { 
            if (n % i == 0) 
                return true; 
        } 
        return false; 
    } 
      
    // Effectively calculate (x^y) modulo mod 
    static int power(int x, int y, int mod) 
    { 
      
        // Initialize result 
        int res = 1; 
      
        while (y != 0) 
        { 
      
            // If power is odd, then update the answer 
            if ((y & 1) == 1) 
                res = (res * x) % mod; 
      
            // Square the number and reduce 
            // the power to its half 
            y = y >> 1; 
            x = (x * x) % mod; 
        } 
      
        // Return the result 
        return res; 
    } 
      
    // Function to check for Fermat Pseudoprime 
    static int Check(int n, int a) 
    { 
      
        // If it is composite and satisfy Fermat criterion 
        if (a > 1 && checkcomposite(n) && 
                     power(a, n - 1, n) == 1) 
            return 1; 
      
        // Else return 0 
        return 0; 
    } 
      
    // Driver code 
    static public void Main ()
    {
        int N = 645; 
        int a = 2; 
      
        // Function call 
        Console.WriteLine(Check(N, a)); 
    }
}
  
// This code is contributed by AnkitRai01


输出:
1

时间复杂度: O(sqrt(N))