Difference between revisions of "Testing for prime"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
<pre id='shellbody' data-qtp='DOM'></pre> | <pre id='shellbody' data-qtp='DOM'></pre> | ||
Fermat's little theorem tells us that | Fermat's little theorem tells us that | ||
x<sup>p</sup> mod p = x | x<sup>p</sup> mod p = x | ||
Line 10: | Line 8: | ||
then p is not prime. If the equality holds for some value of x then p is <i>probably</i> a prime. If the equality holds for two values of x then it is even more probable that p is prime. For these questions you can ignore that probability. | then p is not prime. If the equality holds for some value of x then p is <i>probably</i> a prime. If the equality holds for two values of x then it is even more probable that p is prime. For these questions you can ignore that probability. | ||
==Probable primes== | |||
<div class='qu' data-width=300> | |||
Decide which of these numbers is prime: | Decide which of these numbers is prime: | ||
*12 | *12 | ||
Line 17: | Line 17: | ||
*35563611982942194303 | *35563611982942194303 | ||
*82793885002522383103 | *82793885002522383103 | ||
<pre class='usr'> | <pre class='usr'> | ||
let pl = [ 12n, 101n, 4249599619n, 5175703781n, | let pl = [ 12n, 101n, 4249599619n, 5175703781n, | ||
35563611982942194303n, 82793885002522383103n | 35563611982942194303n, 82793885002522383103n | ||
]; | ]; | ||
document.body.append(... | document.body.append(... | ||
pl.map(n=>{ | pl.map(n=>{ | ||
let isPrime = n% | let isPrime = n%2n===1n; | ||
ret = document.createElement('div'); | ret = document.createElement('div'); | ||
ret.innerHTML = `${n} is prime: ${isPrime}`; | ret.innerHTML = `${n} is prime: ${isPrime}`; | ||
}) | return ret; | ||
}) | |||
); | ); | ||
Line 42: | Line 39: | ||
<pre class='ans'> | <pre class='ans'> | ||
let pl = [ 12n, 101n, 4249599619n, 5175703781n, | let pl = [ 12n, 101n, 4249599619n, 5175703781n, | ||
35563611982942194303n, 82793885002522383103n | 35563611982942194303n, 82793885002522383103n | ||
]; | ]; | ||
document.body.append(... | document.body.append(... | ||
pl.map(n=>{ | pl.map(n=>{ | ||
let isPrime = | let isPrime = n%2n===1n; | ||
ret = document.createElement('div'); | ret = document.createElement('div'); | ||
ret.innerHTML = `${n} is prime: ${isPrime}`; | ret.innerHTML = `${n} is prime: ${isPrime}`; | ||
}) | return ret; | ||
}) | |||
); | ); | ||
Revision as of 20:18, 26 September 2021
Fermat's little theorem tells us that
xp mod p = x
if p
is prime for x<p
The converse is that if
xp mod p ≠ x
then p is not prime. If the equality holds for some value of x then p is probably a prime. If the equality holds for two values of x then it is even more probable that p is prime. For these questions you can ignore that probability.
Probable primes
Decide which of these numbers is prime:
- 12
- 101
- 4249599619
- 5175703781
- 35563611982942194303
- 82793885002522383103
let pl = [ 12n, 101n, 4249599619n, 5175703781n, 35563611982942194303n, 82793885002522383103n ]; document.body.append(... pl.map(n=>{ let isPrime = n%2n===1n; ret = document.createElement('div'); ret.innerHTML = `${n} is prime: ${isPrime}`; return ret; }) ); //raise n to the power e, modulo m function pow(n,e,m){ if (e<=0) return 1n; let r = pow(n,e/2n,m); return (r*r*(e%2n===1n?n:1n))%m; }
let pl = [ 12n, 101n, 4249599619n, 5175703781n, 35563611982942194303n, 82793885002522383103n ]; document.body.append(... pl.map(n=>{ let isPrime = n%2n===1n; ret = document.createElement('div'); ret.innerHTML = `${n} is prime: ${isPrime}`; return ret; }) ); //raise n to the power e, modulo m function pow(n,e,m){ if (e<=0) return 1n; let r = pow(n,e/2n,m); return (r*r*(e%2n===1n?n:1n))%m; }