This commit is contained in:
~d6 2022-02-07 23:07:05 -05:00
parent 05916ba307
commit 601d76df88
1 changed files with 15 additions and 10 deletions

View File

@ -1,22 +1,27 @@
( primes32.tal )
( )
( primes32.tal )
( )
( Uses a simple trial-divion method to find primes. )
( )
( )
( To determine if x is prime we: )
( 1. Check if x is 2 (prime) )
( 2. Check if x is even (not prime) )
( 3. Starting with i=3, we see if x%i is 0 )
( a. We increment i by 2 to avoid even i )
( 3. Check if x is 3 (prime) )
( 4. Starting with i=5, we see if x%i is 0 )
( a. We alternately increment i by 2 and 4 )
( b. We stop when x < i*i or i=0xffff )
( 4. If we didn't find an i, x is prime. )
( )
( 5. If we didn't find an i, x is prime. )
( )
( The reason we alternate our increment is because we )
( know that x%6 must equal 1 or 5. if x%6 was 3 then x )
( would be divisible by 3. )
( )
( This method can be fast for some large composite )
( numbers but is slower for large primes. )
( )
( )
( On my machine, checking 0x7fffffff took 0.5 seconds )
( and checking 0xfffffffb took 0.9 seconds. Both are )
( prime numbers. )
( )
( )
( Smaller primes also run fairly quickly: 0x17b5d was )
( determined to be prime in 0.02 seconds. )
@ -32,7 +37,7 @@
#fffe #0001
OVR2 OVR2 ;is-prime32 JSR2 ( test for primality )
STH ;emit-long JSR2 SPACE STHr EMIT-BYTE NEWLINE ( output )
#00 DIV #00 ( exit with /0 to make timing easier )
#00 #00 DIV ( exit with /0 to make timing easier )
BRK
( include 32-bit math library )