June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,56 @@
with: a
: nextrow \ a -- a
len
[ ( drop [1] ),
( drop [1,1] ),
( ' n:+ y 1 slide 1 push ) ]
swap 2 min caseof ;
;with
with: n
: .x \ n --
dup
[ ( drop ),
( drop "x" . ),
( "x^" . . ) ]
swap 2 min caseof space ;
: .term \ coef exp -- ; omit coef for 1x^n when n > 0
over 1 = over 0 > and if nip .x else swap . .x then ;
: .sgn \ +/-1 --
[ "-", null, "+" ]
swap 1+ caseof . space ;
: .lhs \ n --
"(x-1)^" . . ;
: .rhs \ a -- a
a:len 1- >r
1 swap ( third .sgn r@ rot - .term -1 * ) a:each
nip rdrop ;
: .eqn \ a -- a
a:len 1- .lhs " = " . .rhs ;
: .binomials \ --
[] ( nextrow .eqn cr ) 8 times drop ;
: primerow? \ a -- a ?
a:len 3 < if false ;then
1 a:@ >r \ 2nd position is the number to check for primality
true swap ( nip dup 1 = swap r@ mod 0 = or and ) a:each swap
rdrop ;
: .primes-via-aks \ --
[] ( nextrow primerow? if 1 a:@ . space then ) 50 times drop ;
;with
.binomials cr
"The primes upto 50 are (via AKS): " . .primes-via-aks cr
bye

View file

@ -0,0 +1,82 @@
import extensions.
singleton AksTest
{
static Array<long> c := V<long>(100).
coef(int n)
[
int i := 0.
int j := 0.
if ((n < 0) || (n > 63)) [ AbortException new; raise ]. // gracefully deal with range issue
c[i] := 1l.
while(i < n)
[
j := i.
c[1 + j] := 1l.
while (j > 0)
[
c[j] := c[j - 1] - c[j].
j -= 1
].
c[0] := c[0] negative.
i += 1
].
var t := c.
]
bool is_prime(int n)
[
int i := n.
self coef(n).
(c[0]) += 1.
(c[i]) -= 1.
i -= 1.
while ((i + 1 != 0) && (c[i+1] mod(n) == 0))
[
i -= 1
].
^ i < 0
]
show(int n)
[
int i := n.
i += 1.
while(i != 0)
[
i -= 1.
console print("+",c[i],"x^",i).
]
]
}
public program =
[
0 till:10 do(:n)<int>
[
AksTest coef(n).
console print("(x-1)^",n," = ").
AksTest show(n).
console printLine.
].
console print("Primes:").
1 to(63) do(:n)<int>
[
if (AksTest is_prime(n))
[
console print(n," ").
]
].
console printLine; readLine
].

View file

@ -1,13 +1,20 @@
: nextCoef(prev)
| i |
ListBuffer new dup add(0)
prev size 1- loop: i [ dup add(prev at(i) prev at(i 1+) - ) ]
dup add(0) ;
import: mapping
: coefs(n) [ 0, 1, 0 ] #nextCoef times(n) extract(2, n 2 + ) ;
: isPrime(n) coefs(n) extract(2, n) conform(#[n mod 0 == ]) ;
: nextCoef( prev -- [] )
| i |
Array new 0 over dup
prev size 1- loop: i [ prev at(i) prev at(i 1+) - over add ]
0 over add
;
: coefs( n -- [] )
[ 0, 1, 0 ] #nextCoef times(n) extract(2, n 2 + ) ;
: prime?( n -- b)
coefs( n ) extract(2, n) conform?( #[n mod 0 == ] ) ;
: aks
| i |
0 10 for: i [ System.Out "(x-1)^" << i << " = " << coefs(i) << cr ]
50 seq filter(#isPrime) apply(#[ print " " print ]) printcr ;
0 10 for: i [ System.Out "(x-1)^" << i << " = " << coefs( i ) << cr ]
50 seq filter( #prime? ) apply(#.) printcr
;

View file

@ -0,0 +1,31 @@
constant expansions = [1], [1,-1], -> @prior { [|@prior,0 Z- 0,|@prior] } ... *;
sub polyprime($p where 2..*) { so expansions[$p].[1 ..^ */2].all %% $p }
# Showing the expansions:
say ' p: (x-1)';
say '-----------';
sub super ($n) {
$n.trans: '0123456789'
=> '¹²³';
}
for ^13 -> $d {
say $d.fmt('%2i: '), (
expansions[$d].kv.map: -> $i, $n {
my $p = $d - $i;
[~] gather {
take < + - >[$n < 0] ~ ' ' unless $p == $d;
take $n.abs unless $p == $d > 0;
take 'x' if $p > 0;
take super $p - $i if $p > 1;
}
}
)
}
# And testing the function:
print "\nPrimes up to 100:\n { grep &polyprime, 2..100 }\n";

View file

@ -0,0 +1,9 @@
AKS<-function(p){
i<-2:p-1
l<-unique(factorial(p) / (factorial(p-i) * factorial(i)))
if(all(l%%p==0)){
print(noquote("It is prime."))
}else{
print(noquote("It isn't prime."))
}
}

View file

@ -1,41 +1,42 @@
/*REXX program calculates primes via the Agrawal─Kayal─Saxena (AKS) primality test.*/
parse arg Z .; if Z=='' then Z=200 /*Z not specified? Then use default.*/
OZ=Z; tell= Z<0; Z=abs(Z) /*Is Z negative? Then show expression.*/
numeric digits max(9, Z%3) /*define a dynamic # of decimal digits.*/
$.0='-'; $.1="+"; @.=1 /*$.x: sign char; default coefficients.*/
#= /*define list of prime numbers (so far)*/
do p=3 for Z; pm=p-1; pp=p+1 /*PM & PP: used as a coding convenience*/
do m=2 for pp%2-1; mm=m-1 /*calculate coefficients for a power. */
@.p.m=@.pm.mm + @.pm.m; h=pp-m /*calculate left side of coefficients*/
@.p.h=@.p.m /* " right " " " */
end /*m*/ /* [↑] The M DO loop creates both */
end /*p*/ /* sides in the same loop, saving */
/* a bunch of execution time. */
if tell then say '(x-1)^0: 1' /*possibly display the first expression*/
/* [↓] test for primality by division.*/
do n=2 for Z; nh=n%2; d=n-1 /*create expressions; find the primes.*/
do k=3 to nh while @.n.k//d==0 /*are coefficients divisible by N-1 ? */
end /*k*/ /* [↑] skip the 1st & 2nd coefficients*/
/* [↓] multiple THEN─IF faster than &s*/
if k>nh then if d\==1 then if d\==4 then #=# d /*add a number to prime list.*/
if \tell then iterate /*Don't tell? Don't show expressions.*/
y='(x-1)^'d": " /*define the 1st part of the expression*/
s=1 /*S: is the sign indicator (-1│+1).*/
do j=n for n-1 by -1; jm=j-1 /*create the higher powers first. */
if j==2 then xp='x' /*if power=1, then don't show the power*/
else xp='x^'jm /* ··· else show power with ^ */
if j==n then y=y xp /*no sign (+│-) for the 1st expression.*/
else y=y $.s || @.n.j''xp /*build the expression with sign (+|-).*/
s=\s /*flip the sign for the next expression*/
end /*j*/ /* [↑] the sign (now) is either 0 │ 1,*/
/* and is displayed either - │ + */
say y $.s || 1 /*just show the first N expressions, */
end /*n*/ /* [↑] ··· but only for negative Z. */
say /* [↓] Has Z a leading + ? Then show.*/
if Z==word(. #, words(#)+1) then is= 'is' /*the number is a prime. */
else is= "isn't" /* " " isn't " " */
if left(OZ, 1)=='+' then say Z is 'prime.' /*display if OZ has a + (plus sign).*/
else say 'primes:' # /*display the prime number list. */
say /* [↓] the digit length of a big coef.*/
say 'Found ' words(#) " primes and the largest coefficient has " length(@.pm.h),
" decimal digits." /*stick a fork in it, we're all done. */
parse arg Z .; if Z=='' | Z=="," then Z=200 /*Z not specified? Then use default.*/
OZ=Z; tell= Z<0; Z=abs(Z) /*Is Z negative? Then show expression.*/
numeric digits max(9, Z % 3) /*define a dynamic # of decimal digits.*/
call AKS /*invoke the AKS funtion for coef. bld.*/
if left(OZ,1)=='+' then do; say Z isAksp(); exit /*display if Z is or isn't a prime.*/
end /* [↑] call isAKSp if Z has leading +.*/
say; say "primes found:" # /*display the prime number list. */
say; if \datatype(#, 'W') then exit /* [↓] the digit length of a big coef.*/
say 'Found ' words(#) " primes and the largest coefficient has " length(@.pm.h) @dd
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isAKSp: if z==word(#,words(#)) then return ' is a prime.'; else return " isn't a prime."
/*──────────────────────────────────────────────────────────────────────────────────────*/
AKS: $.0= '-'; $.1= "+"; @.=1 /*$.x: sign char; default coefficients.*/
q.=1; q.1=0; q.4=0 /*sparse array for faster comparisons. */
#=; L= length(Z) /*define list of prime numbers (so far)*/
do p=3 for Z; pm=p - 1; pp=p + 1 /*PM & PP: used as a coding convenience*/
do m=2 for pp % 2 - 1; mm=m - 1 /*calculate coefficients for a power. */
@.p.m= @.pm.mm + @.pm.m; h=pp - m /*calculate left side of coefficients*/
@.p.h= @.p.m /* " right " " " */
end /*m*/ /* [↑] The M DO loop creates both */
end /*p*/ /* sides in the same loop. */
if tell then say '(x-1)^'right(0, L)": 1" /*possibly display the first expression*/
@dd= 'decimal digits.' /* [↓] test for primality by division.*/
do n=2 for Z; nh=n % 2; d=n - 1 /*create expressions; find the primes.*/
do k=3 to nh while @.n.k//d==0 /*are coefficients divisible by N-1 ? */
end /*k*/ /* [↑] skip the 1st & 2nd coefficients*/
if k>nh then if q.d then #=# d /*add a number to the prime list. */
if \tell then iterate /*Don't tell? Don't show expressions.*/
y='(x-1)^'right(d, L)": " /*define the 1st part of the expression*/
s=1 /*S: is the sign indicator (-1│+1).*/
do j=n for n-1 by -1 /*create the higher powers first. */
if j==2 then xp= 'x' /*if power=1, then don't show the power*/
else xp= 'x^' || (j-1) /* ··· else show power with ^ */
if j==n then y=y xp /*no sign (+│-) for the 1st expression.*/
else y=y $.s || @.n.j''xp /*build the expression with sign (+|-).*/
s= \s /*flip the sign for the next expression*/
end /*j*/ /* [↑] the sign (now) is either 0 │ 1,*/
say y $.s'1' /*just show the first N expressions, */
end /*n*/ /* [↑] ··· but only for negative Z. */
if #=='' then #='none'; return # /*if null, return "none"; else return #*/