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

@ -5,10 +5,15 @@
( To determine if x is prime we: ) ( To determine if x is prime we: )
( 1. Check if x is 2 (prime) ) ( 1. Check if x is 2 (prime) )
( 2. Check if x is even (not prime) ) ( 2. Check if x is even (not prime) )
( 3. Starting with i=3, we see if x%i is 0 ) ( 3. Check if x is 3 (prime) )
( a. We increment i by 2 to avoid even i ) ( 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 ) ( 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 ) ( This method can be fast for some large composite )
( numbers but is slower for large primes. ) ( numbers but is slower for large primes. )
@ -32,7 +37,7 @@
#fffe #0001 #fffe #0001
OVR2 OVR2 ;is-prime32 JSR2 ( test for primality ) OVR2 OVR2 ;is-prime32 JSR2 ( test for primality )
STH ;emit-long JSR2 SPACE STHr EMIT-BYTE NEWLINE ( output ) 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 BRK
( include 32-bit math library ) ( include 32-bit math library )