📜  在Modulo p下找到平方根|集合1(当p的形式为4 * i + 3时)

📅  最后修改于: 2021-05-05 01:21:39             🧑  作者: Mango

给定数字“ n”和质数“ p”,请在模p下找到n的平方根(如果存在)。可以假设p的形式为4 * i + 3(OR p%4 = 3),其中i为整数。这样的素数的示例是7、11、19、23、31等
例子:

Input:  n = 2, p = 7
Output: 3 or 4
3 and 4 both are square roots of 2 under modulo
7 because (3*3) % 7 = 2 and (4*4) % 7 = 2

Input:  n = 2, p = 5
Output: Square root doesn't exist

天真的解决方案:尝试从2到p-1的所有数字。对于每个数字x,检查x是否为模p下n的平方根。

C++
// A Simple C++ program to find square root under modulo p
// when p is 7, 11, 19, 23, 31, ... etc,
#include 
using namespace std;
 
// Returns true if square root of n under modulo p exists
void squareRoot(int n, int p)
{
    n = n % p;
 
    // One by one check all numbers from 2 to p-1
    for (int x = 2; x < p; x++) {
        if ((x * x) % p == n) {
            cout << "Square root is " << x;
            return;
        }
    }
    cout << "Square root doesn't exist";
}
 
// Driver program to test
int main()
{
    int p = 7;
    int n = 2;
    squareRoot(n, p);
    return 0;
}


Java
// A Simple Java program to find square
// root under modulo p when p is 7,
// 11, 19, 23, 31, ... etc,
import java .io.*;
 
class GFG {
 
    // Returns true if square root of n
    // under modulo p exists
    static void squareRoot(int n, int p)
    {
        n = n % p;
     
        // One by one check all numbers
        // from 2 to p-1
        for (int x = 2; x < p; x++) {
            if ((x * x) % p == n) {
                System.out.println("Square "
                    + "root is " + x);
                return;
            }
        }
        System.out.println("Square root "
                + "doesn't exist");
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int p = 7;
        int n = 2;
        squareRoot(n, p);
    }
}
 
// This code is contributed by Anuj_67


Python3
# A Simple Python program to find square
# root under modulo p when p is 7, 11,
# 19, 23, 31, ... etc,
 
# Returns true if square root of n under
# modulo p exists
def squareRoot(n, p):
 
    n = n % p
     
    # One by one check all numbers from
    # 2 to p-1
    for x in range (2, p):
        if ((x * x) % p == n) :
            print( "Square root is ", x)
            return
 
    print( "Square root doesn't exist")
 
# Driver program to test
p = 7
n = 2
squareRoot(n, p)
 
# This code is Contributed by Anuj_67


C#
// A Simple C# program to find square
// root under modulo p when p is 7,
// 11, 19, 23, 31, ... etc,
using System;
 
class GFG {
 
    // Returns true if square root of n
    // under modulo p exists
    static void squareRoot(int n, int p)
    {
        n = n % p;
     
        // One by one check all numbers
        // from 2 to p-1
        for (int x = 2; x < p; x++) {
            if ((x * x) % p == n) {
                Console.Write("Square "
                     + "root is " + x);
                return;
            }
        }
        Console.Write("Square root "
                   + "doesn't exist");
    }
     
    // Driver Code
    static void Main()
    {
        int p = 7;
        int n = 2;
        squareRoot(n, p);
    }
}
 
// This code is contributed by Anuj_67


PHP


Javascript


C++
// An efficient C++ program to find square root under
// modulo p when p is 7, 11, 19, 23, 31, ... etc.
#include 
using namespace std;
 
// Utility function to do modular exponentiation.
// It returns (x^y) % p.
int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p; // Update x if it is more than or
    // equal to p
 
    while (y > 0)
    {
       
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Returns true if square root of n under modulo p exists
// Assumption: p is of the form 3*i + 4 where i >= 1
void squareRoot(int n, int p)
{
    if (p % 4 != 3) {
        cout << "Invalid Input";
        return;
    }
 
    // Try "+(n^((p + 1)/4))"
    n = n % p;
    int x = power(n, (p + 1) / 4, p);
    if ((x * x) % p == n) {
        cout << "Square root is " << x;
        return;
    }
 
    // Try "-(n ^ ((p + 1)/4))"
    x = p - x;
    if ((x * x) % p == n) {
        cout << "Square root is " << x;
        return;
    }
 
    // If none of the above two work, then
    // square root doesn't exist
    cout << "Square root doesn't exist ";
}
 
// Driver program to test
int main()
{
    int p = 7;
    int n = 2;
    squareRoot(n, p);
    return 0;
}


Java
// An efficient Java program to find square root under
// modulo p when p is 7, 11, 19, 23, 31, ... etc.
public class GFG {
 
 
// Utility function to do modular exponentiation.
// It returns (x^y) % p.
static int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p; // Update x if it is more than or
    // equal to p
 
    while (y > 0) {
        // If y is odd, multiply x with result
        if (y %2== 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Returns true if square root of n under modulo p exists
// Assumption: p is of the form 3*i + 4 where i >= 1
static void squareRoot(int n, int p)
{
    if (p % 4 != 3) {
        System.out.print("Invalid Input");
        return;
    }
 
    // Try "+(n^((p + 1)/4))"
    n = n % p;
    int x = power(n, (p + 1) / 4, p);
    if ((x * x) % p == n) {
        System.out.print("Square root is " + x);
        return;
    }
 
    // Try "-(n ^ ((p + 1)/4))"
    x = p - x;
    if ((x * x) % p == n) {
        System.out.print("Square root is " + x);
        return;
    }
 
    // If none of the above two work, then
    // square root doesn't exist
    System.out.print("Square root doesn't exist ");
}
 
// Driver program to test
   static public void main(String[] args) {
       int p = 7;
    int n = 2;
    squareRoot(n, p);
    }
}


Python3
# An efficient python3 program to find square root
# under modulo p when p is 7, 11, 19, 23, 31, ... etc.
 
# Utility function to do modular exponentiation.
# It returns (x^y) % p.
def power(x, y, p) :
 
    res = 1 # Initialize result
    x = x % p # Update x if it is more
              # than or equal to p
 
    while (y > 0):
         
        # If y is odd, multiply x with result
        if (y & 1):
            res = (res * x) % p
 
        # y must be even now
        y = y >> 1 # y = y/2
        x = (x * x) % p
 
    return res
 
# Returns true if square root of n under
# modulo p exists. Assumption: p is of the
# form 3*i + 4 where i >= 1
def squareRoot(n, p):
 
    if (p % 4 != 3) :
        print( "Invalid Input" )
        return
 
 
    # Try "+(n^((p + 1)/4))"
    n = n % p
    x = power(n, (p + 1) // 4, p)
    if ((x * x) % p == n):
        print( "Square root is ", x)
        return
 
    # Try "-(n ^ ((p + 1)/4))"
    x = p - x
    if ((x * x) % p == n):
        print( "Square root is ", x )
        return
 
    # If none of the above two work, then
    # square root doesn't exist
    print( "Square root doesn't exist " )
 
# Driver Code
p = 7
n = 2
squareRoot(n, p)
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// An efficient C# program to find square root under
// modulo p when p is 7, 11, 19, 23, 31, ... etc.
 
using System;
public class GFG {
  
// Utility function to do modular exponentiation.
// It returns (x^y) % p.
static int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p; // Update x if it is more than or
    // equal to p
  
    while (y > 0) {
        // If y is odd, multiply x with result
        if (y %2 == 1)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
  
// Returns true if square root of n under modulo p exists
// Assumption: p is of the form 3*i + 4 where i >= 1
static void squareRoot(int n, int p)
{
    if (p % 4 != 3) {
        Console.Write("Invalid Input");
        return;
    }
  
    // Try "+(n^((p + 1)/4))"
    n = n % p;
    int x = power(n, (p + 1) / 4, p);
    if ((x * x) % p == n) {
        Console.Write("Square root is " + x);
        return;
    }
  
    // Try "-(n ^ ((p + 1)/4))"
    x = p - x;
    if ((x * x) % p == n) {
        Console.Write("Square root is " + x);
        return;
    }
  
    // If none of the above two work, then
    // square root doesn't exist
    Console.Write("Square root doesn't exist ");
}
  
// Driver program to test
   static public void Main() {
       int p = 7;
    int n = 2;
    squareRoot(n, p);
    }
}
// This code is contributed by Ita_c.


PHP
 0)
    {
         
        // If y is odd, multiply
        // x with result
        if ($y & 1)
            $res = ($res * $x) % $p;
 
        // y must be even now
        // y = y/2
        $y = $y >> 1;
        $x = ($x * $x) % $p;
    }
    return $res;
}
 
// Returns true if square root
// of n under modulo p exists
// Assumption: p is of the
// form 3*i + 4 where i >= 1
function squareRoot($n, $p)
{
    if ($p % 4 != 3)
    {
        echo "Invalid Input";
        return;
    }
 
    // Try "+(n^((p + 1)/4))"
    $n = $n % $p;
    $x = power($n, ($p + 1) / 4, $p);
    if (($x * $x) % $p == $n)
    {
        echo "Square root is ", $x;
        return;
    }
 
    // Try "-(n ^ ((p + 1)/4))"
    $x = $p - $x;
    if (($x * $x) % $p == $n)
    {
        echo "Square root is ", $x;
        return;
    }
 
    // If none of the above
    // two work, then square
    // root doesn't exist
    echo "Square root doesn't exist ";
}
 
    // Driver Code
    $p = 7;
    $n = 2;
    squareRoot($n, $p);
 
// This code is contributed by ajit
?>


Javascript


输出:

Square root is 3

该解决方案的时间复杂度为O(p)
直接方法:如果p的形式为4 * i + 3,则存在一种求平方根的快速方法。

If n is in the form 4*i + 3 with i >= 1 (OR p % 4 = 3)
And 
If Square root of n exists, then it must be
        ±n(p + 1)/4

下面是上述想法的实现:

C++

// An efficient C++ program to find square root under
// modulo p when p is 7, 11, 19, 23, 31, ... etc.
#include 
using namespace std;
 
// Utility function to do modular exponentiation.
// It returns (x^y) % p.
int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p; // Update x if it is more than or
    // equal to p
 
    while (y > 0)
    {
       
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Returns true if square root of n under modulo p exists
// Assumption: p is of the form 3*i + 4 where i >= 1
void squareRoot(int n, int p)
{
    if (p % 4 != 3) {
        cout << "Invalid Input";
        return;
    }
 
    // Try "+(n^((p + 1)/4))"
    n = n % p;
    int x = power(n, (p + 1) / 4, p);
    if ((x * x) % p == n) {
        cout << "Square root is " << x;
        return;
    }
 
    // Try "-(n ^ ((p + 1)/4))"
    x = p - x;
    if ((x * x) % p == n) {
        cout << "Square root is " << x;
        return;
    }
 
    // If none of the above two work, then
    // square root doesn't exist
    cout << "Square root doesn't exist ";
}
 
// Driver program to test
int main()
{
    int p = 7;
    int n = 2;
    squareRoot(n, p);
    return 0;
}

Java

// An efficient Java program to find square root under
// modulo p when p is 7, 11, 19, 23, 31, ... etc.
public class GFG {
 
 
// Utility function to do modular exponentiation.
// It returns (x^y) % p.
static int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p; // Update x if it is more than or
    // equal to p
 
    while (y > 0) {
        // If y is odd, multiply x with result
        if (y %2== 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Returns true if square root of n under modulo p exists
// Assumption: p is of the form 3*i + 4 where i >= 1
static void squareRoot(int n, int p)
{
    if (p % 4 != 3) {
        System.out.print("Invalid Input");
        return;
    }
 
    // Try "+(n^((p + 1)/4))"
    n = n % p;
    int x = power(n, (p + 1) / 4, p);
    if ((x * x) % p == n) {
        System.out.print("Square root is " + x);
        return;
    }
 
    // Try "-(n ^ ((p + 1)/4))"
    x = p - x;
    if ((x * x) % p == n) {
        System.out.print("Square root is " + x);
        return;
    }
 
    // If none of the above two work, then
    // square root doesn't exist
    System.out.print("Square root doesn't exist ");
}
 
// Driver program to test
   static public void main(String[] args) {
       int p = 7;
    int n = 2;
    squareRoot(n, p);
    }
}

Python3

# An efficient python3 program to find square root
# under modulo p when p is 7, 11, 19, 23, 31, ... etc.
 
# Utility function to do modular exponentiation.
# It returns (x^y) % p.
def power(x, y, p) :
 
    res = 1 # Initialize result
    x = x % p # Update x if it is more
              # than or equal to p
 
    while (y > 0):
         
        # If y is odd, multiply x with result
        if (y & 1):
            res = (res * x) % p
 
        # y must be even now
        y = y >> 1 # y = y/2
        x = (x * x) % p
 
    return res
 
# Returns true if square root of n under
# modulo p exists. Assumption: p is of the
# form 3*i + 4 where i >= 1
def squareRoot(n, p):
 
    if (p % 4 != 3) :
        print( "Invalid Input" )
        return
 
 
    # Try "+(n^((p + 1)/4))"
    n = n % p
    x = power(n, (p + 1) // 4, p)
    if ((x * x) % p == n):
        print( "Square root is ", x)
        return
 
    # Try "-(n ^ ((p + 1)/4))"
    x = p - x
    if ((x * x) % p == n):
        print( "Square root is ", x )
        return
 
    # If none of the above two work, then
    # square root doesn't exist
    print( "Square root doesn't exist " )
 
# Driver Code
p = 7
n = 2
squareRoot(n, p)
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

C#

// An efficient C# program to find square root under
// modulo p when p is 7, 11, 19, 23, 31, ... etc.
 
using System;
public class GFG {
  
// Utility function to do modular exponentiation.
// It returns (x^y) % p.
static int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p; // Update x if it is more than or
    // equal to p
  
    while (y > 0) {
        // If y is odd, multiply x with result
        if (y %2 == 1)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
  
// Returns true if square root of n under modulo p exists
// Assumption: p is of the form 3*i + 4 where i >= 1
static void squareRoot(int n, int p)
{
    if (p % 4 != 3) {
        Console.Write("Invalid Input");
        return;
    }
  
    // Try "+(n^((p + 1)/4))"
    n = n % p;
    int x = power(n, (p + 1) / 4, p);
    if ((x * x) % p == n) {
        Console.Write("Square root is " + x);
        return;
    }
  
    // Try "-(n ^ ((p + 1)/4))"
    x = p - x;
    if ((x * x) % p == n) {
        Console.Write("Square root is " + x);
        return;
    }
  
    // If none of the above two work, then
    // square root doesn't exist
    Console.Write("Square root doesn't exist ");
}
  
// Driver program to test
   static public void Main() {
       int p = 7;
    int n = 2;
    squareRoot(n, p);
    }
}
// This code is contributed by Ita_c.

的PHP

 0)
    {
         
        // If y is odd, multiply
        // x with result
        if ($y & 1)
            $res = ($res * $x) % $p;
 
        // y must be even now
        // y = y/2
        $y = $y >> 1;
        $x = ($x * $x) % $p;
    }
    return $res;
}
 
// Returns true if square root
// of n under modulo p exists
// Assumption: p is of the
// form 3*i + 4 where i >= 1
function squareRoot($n, $p)
{
    if ($p % 4 != 3)
    {
        echo "Invalid Input";
        return;
    }
 
    // Try "+(n^((p + 1)/4))"
    $n = $n % $p;
    $x = power($n, ($p + 1) / 4, $p);
    if (($x * $x) % $p == $n)
    {
        echo "Square root is ", $x;
        return;
    }
 
    // Try "-(n ^ ((p + 1)/4))"
    $x = $p - $x;
    if (($x * $x) % $p == $n)
    {
        echo "Square root is ", $x;
        return;
    }
 
    // If none of the above
    // two work, then square
    // root doesn't exist
    echo "Square root doesn't exist ";
}
 
    // Driver Code
    $p = 7;
    $n = 2;
    squareRoot($n, $p);
 
// This code is contributed by ajit
?>

Java脚本


输出:

Square root is 4

该解决方案的时间复杂度为O(Log p)
这是如何运作的?
我们在上一篇文章中讨论了欧拉准则。

As per Euler's criterion, if square root exists, then 
following condition is true
 n(p-1)/2 % p = 1

Multiplying both sides with n, we get
 n(p+1)/2 % p = n % p  ------ (1)

Let x be the modulo square root. We can write,
  (x * x) ≡ n mod p
  (x * x) ≡ n(p+1)/2  [Using (1) given above]
  (x * x) ≡ n(2i + 2) [Replacing n = 4*i + 3]
        x ≡ ±n(i + 1)  [Taking Square root of both sides]
        x ≡ ±n(p + 1)/4 [Putting 4*i + 3 = p or i = (p-3)/4]