-
Notifications
You must be signed in to change notification settings - Fork 0
/
fermat.js
43 lines (37 loc) · 1.1 KB
/
fermat.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
39
40
41
42
43
/**
* Algorithm which checks the primality using Fermat's test
*
* Input: natural number 1 < n, natural number N
*
* Output: 'yes' if the natural number a was a composite number witness of n;
* if after N we have not obtained a composite number certificate, we return 'no conclusion'
*
* WARNING: choose witness in the range 1 < a < n which are co-prime with n and random.
*/
const utilities = require('../utilities');
const fastPower = require('./fastPower');
function isPrime(n, N) {
for (let trial = 0; trial < N; trial++){
// generate a between 2 and n - 2
let a = Math.floor((Math.random() * (n - 2))) + 2;
// check if common factor exists
if (utilities.GCD(a, n) !== 1) {
// factor was found, therefore composite
return false;
}
// Fermat test
if (fastPower(a, n - 1, n) !== 1) {
// must be composite
return false;
}
}
return true;
}
function fermat(n, N) {
if (isPrime(n, N) === true) {
return 'no';
} else {
return 'yes';
}
};
module.exports = fermat;