📜  费马小定理

📅  最后修改于: 2021-04-27 22:48:56             🧑  作者: Mango

费马小定理指出,如果p是素数,那么对于任何整数a,数字a p – a是p的整数倍。

特殊情况:如果a不能被p整除,则Fermat的小定理等同于p-1 -1是p的整数倍的陈述。

举个例子,费马小定理是如何工作的
例子:

P = an integer Prime number   
 a = an integer which is not multiple of P  
 Let a = 2 and P = 17 
 
 According to Fermat's little theorem 
  2 17 - 1     ≡ 1 mod(17)
 we got  65536 % 17 ≡ 1   
 that mean (65536-1) is an multiple of 17 

费马小定理的使用
如果我们知道m是素数,那么我们也可以使用Fermats的小定理来找到逆定理。
一个m- 1≡1(mod m)
如果将两边都乘以-1 ,我们得到
一个-1≡A M-2(mod M)表示
下面是上面的实现

C++
// C++ program to find modular inverse of a
// under modulo m using Fermat's little theorem.
// This program works only if m is prime.
#include 
using namespace std;
 
// To compute x raised to power y under modulo m
int power(int x, unsigned int y, unsigned int m);
 
// Function to find modular inverse of a under modulo m
// Assumption: m is prime
void modInverse(int a, int m)
{
    if (__gcd(a, m) != 1)
        cout << "Inverse doesn't exist";
 
    else {
 
        // If a and m are relatively prime, then
        // modulo inverse is a^(m-2) mode m
        cout << "Modular multiplicative inverse is "
             << power(a, m - 2, m);
    }
}
 
// To compute x^y under modulo m
int power(int x, unsigned int y, unsigned int m)
{
    if (y == 0)
        return 1;
    int p = power(x, y / 2, m) % m;
    p = (p * p) % m;
 
    return (y % 2 == 0) ? p : (x * p) % m;
}
 
// Driver Program
int main()
{
    int a = 3, m = 11;
    modInverse(a, m);
    return 0;
}


Java
// Java program to find modular
// inverse of a under modulo m
// using Fermat's little theorem.
// This program works only if m is prime.
 
class GFG
{
    static int __gcd(int a, int b)
    {
     
        if(b == 0)
        {
            return a;
        }
        else
        {
            return __gcd(b, a % b);
        }
    }
     
    // To compute x^y under modulo m
    static int power(int x,int y,int m)
    {
        if (y == 0)
            return 1;
        int p = power(x, y / 2, m) % m;
        p = (p * p) % m;
     
        return (y % 2 == 0) ? p : (x * p) % m;
    }
     
    // Function to find modular
    // inverse of a under modulo m
    // Assumption: m is prime
    static void modInverse(int a, int m)
    {
        if (__gcd(a, m) != 1)
            System.out.print("Inverse doesn't exist");
     
        else {
     
            // If a and m are relatively prime, then
            // modulo inverse is a^(m-2) mode m
            System.out.print("Modular multiplicative inverse is "
                                            +power(a, m - 2, m));
        }
    }
     
     
    // Driver code
    public static void main (String[] args)
    {
        int a = 3, m = 11;
        modInverse(a, m);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# modular inverse of a
# under modulo m using
# Fermat's little theorem.
# This program works
# only if m is prime.
 
def __gcd(a,b):
 
    if(b == 0):
        return a
    else:
        return __gcd(b, a % b)
     
# To compute x^y under modulo m
def power(x,y,m):
 
    if (y == 0):
        return 1
    p = power(x, y // 2, m) % m
    p = (p * p) % m
  
    return p if(y % 2 == 0) else  (x * p) % m
 
# Function to find modular
# inverse of a under modulo m
# Assumption: m is prime
def modInverse(a,m):
 
    if (__gcd(a, m) != 1):
        print("Inverse doesn't exist")
  
    else:
  
        # If a and m are relatively prime, then
        # modulo inverse is a^(m-2) mode m
        print("Modular multiplicative inverse is ",
             power(a, m - 2, m))
 
# Driver code
 
a = 3
m = 11
modInverse(a, m)
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find modular
// inverse of a under modulo m
// using Fermat's little theorem.
// This program works only if m is prime.
using System;
 
class GFG
{
    static int __gcd(int a, int b)
    {
     
        if(b == 0)
        {
            return a;
        }
        else
        {
            return __gcd(b, a % b);
        }
    }
     
    // To compute x^y under modulo m
    static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;
        int p = power(x, y / 2, m) % m;
        p = (p * p) % m;
     
        return (y % 2 == 0) ? p : (x * p) % m;
    }
     
    // Function to find modular
    // inverse of a under modulo m
    // Assumption: m is prime
    static void modInverse(int a, int m)
    {
        if (__gcd(a, m) != 1)
            Console.WriteLine("Modular multiplicative inverse is "
                                            +power(a, m - 2, m));
     
        else {
     
            // If a and m are relatively prime, then
            // modulo inverse is a^(m-2) mode m
            Console.WriteLine("Modular multiplicative inverse is "
                                            +power(a, m - 2, m));
        }
    }
     
     
    // Driver code
    public static void Main ()
    {
        int a = 3, m = 11;
        modInverse(a, m);
    }
}
 
// This code is contributed by vt_m.


PHP
 $b)
        return __gcd($a-$b, $b);
    return __gcd($a, $b-$a);
}
 
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
function modInverse($a, $m)
{
    if (__gcd($a, $m) != 1)
        echo "Inverse doesn't exist";
 
    else
    {
 
        // If a and m are relatively
        // prime, then modulo inverse
        // is a^(m-2) mode m
        echo "Modular multiplicative inverse is ",
                             power($a,$m - 2, $m);
    }
}
 
// To compute x^y under modulo m
function power($x, $y, $m)
{
    if ($y == 0)
        return 1;
    $p = power($x,$y / 2, $m) % $m;
    $p = ($p * $p) % $m;
 
    return ($y % 2 == 0) ? $p : ($x * $p) % $m;
}
 
    // Driver Code
    $a = 3; $m = 11;
    modInverse($a, $m);
     
// This code is contributed by anuj__67.
?>


Javascript


输出 :

Modular multiplicative inverse is 4

一些基于费马小定理的文章

  • 计算nCr%p |第三套(使用费马小定理)
  • 模乘逆
  • 原始性测试|套装2(Fermat方法)
  • 模数10 ^ 9 + 7(1000000007)