Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
Last Updated :
03 Oct, 2022
Given a number ‘n’ and a prime ‘p’, find square root of n under modulo p if it exists. It may be given that p is in the form for 4*i + 3 (OR p % 4 = 3) where i is an integer. Examples of such primes are 7, 11, 19, 23, 31, … etc,
Examples:
Input: n = 2, p = 7
Output: 3 or 4
Explanation: 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
Naive Solution: Try all numbers from 2 to p-1. And for every number x, check if x is the square root of n under modulo p.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void squareRoot( int n, int p)
{
n = n % p;
for ( int x = 2; x < p; x++) {
if ((x * x) % p == n) {
cout << "Square root is " << x;
return ;
}
}
cout << "Square root doesn't exist" ;
}
int main()
{
int p = 7;
int n = 2;
squareRoot(n, p);
return 0;
}
|
Java
import java .io.*;
class GFG {
static void squareRoot( int n, int p)
{
n = n % p;
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" );
}
public static void main(String[] args)
{
int p = 7 ;
int n = 2 ;
squareRoot(n, p);
}
}
|
Python3
def squareRoot(n, p):
n = n % p
for x in range ( 2 , p):
if ((x * x) % p = = n) :
print ( "Square root is " , x)
return
print ( "Square root doesn't exist" )
p = 7
n = 2
squareRoot(n, p)
|
C#
using System;
class GFG {
static void squareRoot( int n, int p)
{
n = n % p;
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" );
}
static void Main()
{
int p = 7;
int n = 2;
squareRoot(n, p);
}
}
|
PHP
<?php
function squareRoot( $n , $p )
{
$n = $n % $p ;
for ( $x = 2; $x < $p ; $x ++)
{
if (( $x * $x ) % $p == $n )
{
echo ( "Square root is " . $x );
return ;
}
}
echo ( "Square root doesn't exist" );
}
$p = 7;
$n = 2;
squareRoot( $n , $p );
?>
|
Javascript
<script>
function squareRoot(n,p)
{
n = n % p;
for (let x = 2; x < p; x++) {
if ((x * x) % p == n) {
document.write( "Square "
+ "root is " + x);
return ;
}
}
document.write( "Square root "
+ "doesn't exist" );
}
let p = 7;
let n = 2;
squareRoot(n, p);
</script>
|
Time Complexity: O(p)
Auxiliary Space: O(1)
Direct Method: If p is in the form of 4*i + 3, then there exist a Quick way of finding square root.
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
Below is the implementation of the above idea :
C++
#include <iostream>
using namespace std;
int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void squareRoot( int n, int p)
{
if (p % 4 != 3) {
cout << "Invalid Input" ;
return ;
}
n = n % p;
int x = power(n, (p + 1) / 4, p);
if ((x * x) % p == n) {
cout << "Square root is " << x;
return ;
}
x = p - x;
if ((x * x) % p == n) {
cout << "Square root is " << x;
return ;
}
cout << "Square root doesn't exist " ;
}
int main()
{
int p = 7;
int n = 2;
squareRoot(n, p);
return 0;
}
|
Java
public class GFG {
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 ) {
if (y % 2 == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static void squareRoot( int n, int p)
{
if (p % 4 != 3 ) {
System.out.print( "Invalid Input" );
return ;
}
n = n % p;
int x = power(n, (p + 1 ) / 4 , p);
if ((x * x) % p == n) {
System.out.print( "Square root is " + x);
return ;
}
x = p - x;
if ((x * x) % p == n) {
System.out.print( "Square root is " + x);
return ;
}
System.out.print( "Square root doesn't exist " );
}
static public void main(String[] args) {
int p = 7 ;
int n = 2 ;
squareRoot(n, p);
}
}
|
Python3
def power(x, y, p) :
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def squareRoot(n, p):
if (p % 4 ! = 3 ) :
print ( "Invalid Input" )
return
n = n % p
x = power(n, (p + 1 ) / / 4 , p)
if ((x * x) % p = = n):
print ( "Square root is " , x)
return
x = p - x
if ((x * x) % p = = n):
print ( "Square root is " , x )
return
print ( "Square root doesn't exist " )
p = 7
n = 2
squareRoot(n, p)
|
C#
using System;
public class GFG {
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y %2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void squareRoot( int n, int p)
{
if (p % 4 != 3) {
Console.Write( "Invalid Input" );
return ;
}
n = n % p;
int x = power(n, (p + 1) / 4, p);
if ((x * x) % p == n) {
Console.Write( "Square root is " + x);
return ;
}
x = p - x;
if ((x * x) % p == n) {
Console.Write( "Square root is " + x);
return ;
}
Console.Write( "Square root doesn't exist " );
}
static public void Main() {
int p = 7;
int n = 2;
squareRoot(n, p);
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function squareRoot( $n , $p )
{
if ( $p % 4 != 3)
{
echo "Invalid Input" ;
return ;
}
$n = $n % $p ;
$x = power( $n , ( $p + 1) / 4, $p );
if (( $x * $x ) % $p == $n )
{
echo "Square root is " , $x ;
return ;
}
$x = $p - $x ;
if (( $x * $x ) % $p == $n )
{
echo "Square root is " , $x ;
return ;
}
echo "Square root doesn't exist " ;
}
$p = 7;
$n = 2;
squareRoot( $n , $p );
?>
|
Javascript
<script>
function power(x,y,p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if (y %2== 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function squareRoot(n, p)
{
if (p % 4 != 3)
{
document.write( "Invalid Input" );
return ;
}
n = n % p;
let x = power(n, Math.floor((p + 1) / 4), p);
if ((x * x) % p == n) {
document.write( "Square root is " + x);
return ;
}
x = p - x;
if ((x * x) % p == n) {
document.write( "Square root is " + x);
return ;
}
document.write( "Square root doesn't exist " );
}
let p = 7;
let n = 2;
squareRoot(n, p);
</script>
|
Time Complexity: O(Log p)
Auxiliary Space: O(1)
How does this work?
We have discussed Euler’s Criterion in the previous post.
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]
We will soon be discussing methods when p is not in above form.
Share your thoughts in the comments
Please Login to comment...