primes32 update

This commit is contained in:
~d6 2023-11-01 23:03:10 -04:00
parent 436f6dc7bf
commit 1d0bb3da6e
1 changed files with 29 additions and 40 deletions

View File

@ -26,18 +26,15 @@
( Smaller primes also run fairly quickly: 0x17b5d was ) ( Smaller primes also run fairly quickly: 0x17b5d was )
( determined to be prime in 0.02 seconds. ) ( determined to be prime in 0.02 seconds. )
%SP { #2018 DEO }
%NL { #0a18 DEO }
%EXIT { #ff0f DEO BRK }
%DUP4 { OVR2 OVR2 } %DUP4 { OVR2 OVR2 }
%POP4 { POP2 POP2 } %POP4 { POP2 POP2 }
|0100 |0100
( number to check comes first ) #ffff #fffb ( n** ; number to check )
#ffff #fffb DUP4 is-prime32 ( n** is-prime^ ; test for primality )
DUP4 ;is-prime32 JSR2 ( test for primality ) STH emit/long #2018 DEO ( [is-prime^] ; emit n )
STH ;emit/long JSR2 SP STHr ;emit/byte JSR2 NL STHr emit/byte #0a18 DEO ( ; emit boolean )
EXIT #ff0f DEO BRK ( ; exit )
( include 32-bit math library ) ( include 32-bit math library )
~math32.tal ~math32.tal
@ -45,37 +42,29 @@
( return 01 if x is a prime number, else 00 ) ( return 01 if x is a prime number, else 00 )
( works for x >= 2 ) ( works for x >= 2 )
@is-prime32 ( x** -> bool^ ) @is-prime32 ( x** -> bool^ )
,&x1 STR2 ,&x0 STR2 ,&x0 LDR2 ,&x1 LDR2 ( store and reload x ) DUP4 ,&x1 STR2 ,&x0 STR2 ( x** ; store x )
DUP4 #0000 LIT2 &two 0002 ;ne32 JSR2 ( x is 2? ) DUP4 #0000 #0002 ne32 ?{ POP4 #01 JMP2r } ( x** ; return if 2 )
,&not-two JCN POP4 #01 JMP2r ( 2 is prime ) DUP #01 AND ?{ POP4 #00 JMP2r } ( x** ; return if even )
&not-two DUP #01 AND ( x x&1 ) DUP4 #0000 #0003 ne32 ?{ POP4 #01 JMP2r } ( x** ; return if 3 )
,&not-even JCN POP4 #00 JMP2r ( x is even: not prime ) #0002 ,&inc STR2 ( x** ; inc<-2 )
&not-even DUP4 #0000 #0003 ;ne32 JSR2 ( x is 3? ) #0000 #0005 DUP4 ,&i1 STR2 ,&i0 STR2 ( x** i** ; i<-5 )
,&not-three JCN POP4 #01 JMP2r ( 3 is prime ) &loop ( x** i** )
&not-three #0000 ,&i0 STR2 #0005 ,&i1 STR2 ( x i<-5 ) LIT2 [ &x0 $2 ] LIT2 [ &x1 $2 ] ( x** i** x** )
,&two LDR2 ,&inc STR2 LIT2 [ &i0 $2 ] LIT2 [ &i1 $2 ] ( x** i** x** i** )
,&i0 LDR2 ,&i1 LDR2 ( load our candidate, i ) DUP4 mul32 lt32 ( x** i** x<ii^ )
,&loop JMP ( jump over register data to loop label ) STH DUP2 #ffff EQU2 STHr ORA ( x** i** x<ii|i=0xffff^ )
[ &i0 0000 &i1 0000 &x0 0000 &x1 0000 &inc 0000 &mask 0006 ] ( registers ) ?{ ( x** i** )
&loop ( x i ) ,&x0 LDR2 ,&x1 LDR2 ,&i0 LDR2 ,&i1 LDR2 ( x** i** x** i** )
,&x0 LDR2 ,&x1 LDR2 ,&i0 LDR2 ,&i1 LDR2 ( x i x i ) mod32 is-zero32 ( x** i** x//i^ )
DUP4 ;mul32 JSR2 ;lt32 JSR2 ( x i x<i*i ) STH #0000 ,&inc LDR2 add32 ( x** i+2** [x//i^] )
STH DUP2 #ffff EQU2 STHr ORA ( x i x<i*i||i=0xffff ) LIT2 [ &inc $2 ] #0006 EOR2 ,&inc STR2 ( x** i+2** [x//i^] ; inc<-inc^6 )
,&finished JCN ( x i ) DUP4 ,&i1 STR2 ,&i0 STR2 STHr ( x** i+2** x//i^ ; i<-i+2 )
,&x0 LDR2 ,&x1 LDR2 ,&i0 LDR2 ,&i1 LDR2 ( x i x i ) ?{ !&loop } ( x** i+2** ; if x<j*j, loop )
;mod32 JSR2 ;is-zero32 JSR2 ( x i x//i^ ) POP4 POP4 #00 JMP2r ( 0^ ; since i divides x, not prime )
STH #0000 ,&inc LDR2 ;add32 JSR2 ( x i+2 ) } POP4 POP4 #01 JMP2r ( 1^ ; didn't find divisors, prime )
,&inc LDR2 ,&mask LDR2 EOR2 ,&inc STR2 ( inc<-inc^6 )
,&i1 STR2 ,&i0 STR2 ,&i0 LDR2 ,&i1 LDR2 ( write i+2 to register )
STHr ( x i+2 x//i^ )
,&i-divides-x JCN ( x j )
,&loop JMP ( if x<j*j, loop )
&i-divides-x POP4 POP4 #00 JMP2r ( since i divides x, not prime )
&finished POP4 POP4 #01 JMP2r ( didn't find divisors, prime )
@emit @emit
&long SWP2 ,&short JSR &long SWP2 emit/short
&short SWP ,&byte JSR &short SWP emit/byte
&byte DUP #04 SFT ,&char JSR &byte DUP #04 SFT emit/char
&char #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO &char #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO JMP2r
JMP2r