Euler’s criterion (Check if square root under modulo p exists)
Last Updated :
01 Mar, 2023
Given a number ‘n’ and a prime p, find if square root of n under modulo p exists or not. A number x is square root of n under modulo p if (x*x)%p = n%p.
Examples :
Input: n = 2, p = 5
Output: false
There doesn't exist a number x such that
(x*x)%5 is 2
Input: n = 2, p = 7
Output: true
There exists a number x such that (x*x)%7 is
2. The number is 3.
A Naive Method is to try every number x where x varies from 2 to p-1. For every x, check if (x * x) % p is equal to n % p.
C++
#include<iostream>
using namespace std;
bool squareRootExists( int n, int p)
{
n = n%p;
for ( int x=2; x<p; x++)
if ((x*x)%p == n)
return true ;
return false ;
}
int main()
{
int p = 7;
int n = 2;
squareRootExists(n, p)? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean squareRootExists( int n, int p)
{
n = n % p;
for ( int x = 2 ; x < p; x++)
if ((x * x) % p == n)
return true ;
return false ;
}
public static void main (String[] args)
{
int p = 7 ;
int n = 2 ;
if (squareRootExists(n, p))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def squareRootExists(n, p):
n = n % p
for x in range ( 2 , p, 1 ):
if ((x * x) % p = = n):
return True
return False
if __name__ = = '__main__' :
p = 7
n = 2
if (squareRootExists(n, p) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool squareRootExists( int n,
int p)
{
n = n % p;
for ( int x = 2; x < p; x++)
if ((x * x) % p == n)
return true ;
return false ;
}
public static void Main ()
{
int p = 7;
int n = 2;
if (squareRootExists(n, p))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function squareRootExists( $n , $p )
{
$n = $n % $p ;
for ( $x = 2; $x < $p ; $x ++)
if (( $x * $x ) % $p == $n )
return true;
return false;
}
$p = 7;
$n = 2;
if (squareRootExists( $n , $p ) == true)
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function squareRootExists(n, p)
{
n = n % p;
for (let x = 2; x < p; x++)
if ((x * x) % p == n)
return true ;
return false ;
}
let p = 7;
let n = 2;
if (squareRootExists(n, p) === true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
Yes
Time Complexity of this method is O(p).
Space Complexity: O(1) since only constant variables being used
This problem has a direct solution based on Euler’s Criterion.
Euler’s criterion states that
Square root of n under modulo p exists if and only if
n(p-1)/2 % p = 1
Here square root of n exists means is, there exist
an integer x such that (x * x) % p = 1
Below is implementation based on above criterion. Refer Modular Exponentiation for power function.
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;
}
bool squareRootExists( int n, int p)
{
if (power(n, (p-1)/2, p) == 1)
return true ;
return false ;
}
int main()
{
int p = 7;
int n = 2;
squareRootExists(n, p)? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if ((y & 1 ) == 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static boolean squareRootExists( int n,
int p)
{
if (power(n, (p - 1 ) / 2 , p) == 1 )
return true ;
return false ;
}
public static void main (String[] args)
{
int p = 7 ;
int n = 2 ;
if (squareRootExists(n, p))
System.out.println ( "Yes" );
else
System.out.println( "No" );
}
}
|
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 squareRootExists(n, p):
if (power(n, ( int )((p - 1 ) / 2 ), p) = = 1 ):
return True
return False
p = 7
n = 2
if (squareRootExists(n, p) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int power( int x,
int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) == 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static bool squareRootExists( int n,
int p)
{
if (power(n, (p - 1) / 2, p) == 1)
return true ;
return false ;
}
static public void Main ()
{
int p = 7;
int n = 2;
if (squareRootExists(n, p))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
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 squareRootExists( $n , $p )
{
if (power( $n , (int)(( $p - 1) / 2),
$p ) == 1)
return true;
return false;
}
$p = 7;
$n = 2;
if (squareRootExists( $n , $p ) == true)
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) == 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function squareRootExists(n, p)
{
if (power(n, (p - 1) / 2, p) == 1)
return true ;
return false ;
}
let p = 7;
let n = 2;
if (squareRootExists(n, p))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
Yes
How does this work?
If p is a prime, then it must be an odd number and (p-1)
must be an even, i.e., (p-1)/2 must be an integer.
Suppose a square root of n under modulo p exists, then
there must exist an integer x such that,
x2 % p = n % p
or,
x2 ? n mod p
Raising both sides to power (p-1)/2,
(x2)(p-1)/2 ? n(p-1)/2 mod p
xp-1 ? n(p-1)/2 mod p
Since p is a prime, from Fermat's theorem, we can say that
xp-1 ? 1 mod p
Therefore,
n(p-1)/2 ? 1 mod p
Time Complexity: O(logp)
Auxiliary Space: O(1)
You may like to see below:
Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...