I just tried to implement Fermat's little theorem in JavaScript. I tried it both ways, a^(p-1) mod p = 1 and a^p mod p = a mod p.
function fermat(a, p) {
return (((a ^ (p - 1)) % p) === 1);
}
and
function fermat(a, p) {
return ( ( a^p ) % p ) === ( a % p );
}
It doesn't work both ways, is there any way to fix that?
In Javascript ^
means XOR. For exponentiation you need Math.pow(x, y)
.
function fermat(a, p) {
return Math.pow(a, p - 1) % p === 1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With