- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Square Root under Modulo p (When p is in form of 4*i + 3) in C++
In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p (When p is in form of 4*i + 3). Here, p is of the form (4*i + 3) i.e. p % 4 = 3 for i > 1 and p being a prime number.
Here are some numbers, 7, 11, 19, 23, 31...
Let's take an example to understand the problem,
Input : n = 3, p = 7 Output :
Solution Approach
A simple solution to the problem is using a loop. We will loop from 2 to (p - 1). And for every value, check if its square is square root under modulo p is n.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; void findSquareRootMod(int n, int p) { n = n % p; for (int i = 2; i < p; i++) { if ((i * i) % p == n) { cout<<"Square root under modulo is "<<i; return; } } cout<<"Square root doesn't exist"; } int main(){ int p = 11; int n = 3; findSquareRootMod(n, p); return 0; }
Output
Square root under modulo is 5
One more method is directly using the formula,
If p is of the form (4*i + 3) and for square root to exist it will be $+/-n^{(p+1)/4}$
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int calcPowerVal(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y /= 2; x = (x * x) % p; } return res; } void squareRoot(int n, int p) { if (p % 4 != 3) { cout << "Invalid Input"; return; } n = n % p; int sr = calcPowerVal(n, (p + 1) / 4, p); if ((sr * sr) % p == n) { cout<<"Square root under modulo is "<<sr; return; } sr = p - sr; if ((sr * sr) % p == n) { cout << "Square root is "<<sr; return; } cout<<"Square root doesn't exist "; } int main() { int p = 11; int n = 4; squareRoot(n, p); return 0; }
Output
Square root under modulo is 9
- Related Articles
- Find Square Root under Modulo p (Shanks Tonelli algorithm) in C++
- Simplify and verify the result for $p=1$.$4 p^{3} times 3 p^{4} times( -p^{5})$"\n
- If Q varies directly as P and Q = 28 when P = 4, thena) Write an equation connecting P and Qb) Find the value of O when P = 5 andc) Find the value of P when Q = 42.
- Solve: $(-3)^{4}p( frac{-2}{3})^{4}$.
- Factorise ( 16(2 p-3 q)^{2}-4(2 p-3 q) ).
- Simplify ( frac{3^{9} times p^{7}}{9^{3} times p^{4}} ).
- Find the zero of the polynomial in each of the following cases:(i) ( p(x)=x+5 )(ii) ( p(x)=x-5 )(iii) ( p(x)=2 x+5 )(iv) ( p(x)=3 x-2 )(v) ( p(x)=3 x )(vi) ( p(x)=a x, a ≠ 0 )(vii) ( p(x)=c x+d, c ≠ 0, c, d ) are real numbers.
- Subtract ( 4 p^{2} q-3 p q+5 p q^{2}-8 p+7 q-10 ) from ( 18-3 p-11 q+5 p q-2 p q^{2}+5 p^{2} q ).
- Find ( p(0), p(1) ) and ( p(2) ) for each of the following polynomials:(i) ( p(y)=y^{2}-y+1 )(ii) ( p(t)=2+t+2 t^{2}-t^{3} )(iii) ( p(x)=x^{3} )(iv) ( p(x)=(x-1)(x+1) )
- If the point $A( 0,2)$ is equidistant from the points $B( 3, p)$ and $C( p, 5)$, find P. Also find the length of AB.
- In figure below, ( P Q R S ) is a square of side ( 4 mathrm{~cm} ). Find the area of the shaded square."\n
- A point $A( 0, 2)$ is equidistant from the points $B( 3, p)$ and $C( p, 5)$, then find the value of P.
- Difference between ++*p, *p++ and *++p in c++
- Difference between ++*p, *p++ and *++p in C
- What Is The Square Root Of 4

Advertisements