Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
23
Task/AKS-test-for-primes/JavaScript/aks-test-for-primes-2.js
Normal file
23
Task/AKS-test-for-primes/JavaScript/aks-test-for-primes-2.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function pascal(n) {
|
||||
var cs = []; if (n) while (n--) coef(); return coef
|
||||
function coef() {
|
||||
if (cs.length === 0) return cs = [1];
|
||||
for (var t=[1,1], i=cs.length-1; i; i-=1) t.splice( 1, 0, cs[i-1]+cs[i] ); return cs = t
|
||||
}
|
||||
}
|
||||
|
||||
function show(cs) {
|
||||
for (var s='', sgn=true, i=0, deg=cs.length-1; i<=deg; sgn=!sgn, i+=1) {
|
||||
s += ' ' + (sgn ? '+' : '-') + cs[i] + (e => e==0 ? '' : e==1 ? 'x' : 'x<sup>' + e + '</sup>')(deg-i)
|
||||
}
|
||||
return '(x-1)<sup>' + deg + '</sup> =' + s;
|
||||
}
|
||||
|
||||
function isPrime(cs) {
|
||||
var deg=cs.length-1; return cs.slice(1, deg).every( function(c) { return c % deg === 0 } )
|
||||
}
|
||||
|
||||
var coef=pascal(); for (var i=0; i<=7; i+=1) document.write(show(coef()), '<br>')
|
||||
|
||||
document.write('<br>Primes: ');
|
||||
for (var coef=pascal(2), n=2; n<=50; n+=1) if (isPrime(coef())) document.write(' ', n)
|
||||
22
Task/AKS-test-for-primes/JavaScript/aks-test-for-primes-3.js
Normal file
22
Task/AKS-test-for-primes/JavaScript/aks-test-for-primes-3.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function coef(n) {
|
||||
for (var c=[1], i=0; i<n; c[0]=-c[0], i+=1) {
|
||||
c[i+1]=1; for (var j=i; j; j-=1) c[j] = c[j-1]-c[j]
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
function show(cs) {
|
||||
var s='', n=cs.length-1
|
||||
do s += (cs[n]>0 ? ' +' : ' ') + cs[n] + (n==0 ? '' : n==1 ? 'x' :'x<sup>'+n+'</sup>'); while (n--)
|
||||
return s
|
||||
}
|
||||
|
||||
function isPrime(n) {
|
||||
var cs=coef(n), i=n-1; while (i-- && cs[i]%n == 0);
|
||||
return i < 1
|
||||
}
|
||||
|
||||
for (var n=0; n<=7; n++) document.write('(x-1)<sup>',n,'</sup> = ', show(coef(n)), '<br>')
|
||||
|
||||
document.write('<br>Primes: ');
|
||||
for (var n=2; n<=50; n++) if (isPrime(n)) document.write(' ', n)
|
||||
Loading…
Add table
Add a link
Reference in a new issue