-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquarePrime.js
38 lines (31 loc) · 838 Bytes
/
squarePrime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Algorithm which finds a square root of a square modulo p
*
* Input: prime number p congruent to 3 modulo 4, natural number 1 < m < p
*
* Output:
* if m is not a square modulo p, return 0 (or empty array),
* if m is a square modulo p, return a number 1 < b < p such that p | (b2 − a)
*
* Hint: apply Euler theorem.
*/
const utilities = require('../utilities');
function isCongruent(p) {
if (p % 4 == 3)
return true;
else
return false;
}
function squarePrime(p, m) {
p = utilities.convertToDecimal(p.join(''));
m = utilities.convertToDecimal(m.join(''));
if (!isCongruent) return [];
m = m % p;
for (let i = 2; i < p; i++) {
if ((i * i) % p == m) {
return utilities.convertToBinary(i);
}
}
return [];
}
module.exports = squarePrime;