• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] IsPrime

This is not like all the other primality test functions.
The difference with this one is that it attempts to do a few smart things before it goes for the brute force approach.

JASS:
/**************************************
*
*	IsPrime
*	v1.0.0.0
*	By Magtheridon96
*
*	- Efficient Primality Test Function
*
*	Requires:
*	---------
*
*		- Table by Bribe
*			- [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
*
*	API:
*	----
*
*		function IsPrime takes integer n returns boolean
*			- Determines whether a number is prime or not.
*
**************************************/
library IsPrime requires Table

    globals
        private integer array pList
        private Table prime
        private Table done
    endglobals

    function IsPrime takes integer n returns boolean
        local integer i = 0
        local integer k = 0
        local boolean notFound = true
        
        /*
        *	Shift to positive integer.
        */
        if n < 0 then
        	set n = -n
        endif
        
        /*
        *	No need to repeat previous computations.
        */
        if n <= 100 or done.boolean[n] then
        	return prime.boolean[n]
        endif
        
        set done.boolean[n] = true
        
    	/*
       	*	Quick test.
       	*
       	*	This gets rid of most numbers.
       	*/
       	if n/2*2 == 0 or n/3*3 == 0 or n/5*5 == 0 then
       		set prime.boolean[n] = false
       		return false
       	endif
        
        /*
        *	If the number is not of the form 
        *	30k + i where i is a prime between 
    	*	1 and 29 not equal to 2, 3 or 5, 
        *	then the number is not prime.
        *
        *	All primes are of this form.
        */
        loop
        	set k = n - pList[i]
        	if k/30*30 == k then
        		set notFound = false
        		exitwhen true
        	endif
        	exitwhen i == 7
        	set i = i + 1
        endloop
        
        if notFound then
        	set prime.boolean[n] = false
        	return false
        endif
        
        /*
        *	Brute force test.
        *
        *	This is very slow ;_;
        */
        set i = 7
    	set k = R2I(SquareRoot(n))
    	loop
       		if n/i*i == n then
       			set prime.boolean[n] = false
       			return false
       		endif
       		exitwhen i >= k
       		set i = i + 2
       	endloop
       	
       	set prime.boolean[n] = true
       	return true
	endfunction
	
    private module Init
        private static method onInit takes nothing returns nothing
        	set done = Table.create()
        	set prime = Table.create()
        	
            set prime.boolean[0]  = true
            set prime.boolean[1]  = true
            set prime.boolean[2]  = true
            set prime.boolean[3]  = true
            set prime.boolean[5]  = true
            set prime.boolean[7]  = true
            set prime.boolean[11] = true
            set prime.boolean[13] = true
            set prime.boolean[17] = true
            set prime.boolean[19] = true
            set prime.boolean[23] = true
            set prime.boolean[29] = true
            set prime.boolean[31] = true
            set prime.boolean[37] = true
            set prime.boolean[41] = true
            set prime.boolean[43] = true
            set prime.boolean[47] = true
            set prime.boolean[53] = true
            set prime.boolean[59] = true
            set prime.boolean[61] = true
            set prime.boolean[67] = true
            set prime.boolean[71] = true
            set prime.boolean[73] = true
            set prime.boolean[79] = true
            set prime.boolean[83] = true
            set prime.boolean[89] = true
            set prime.boolean[97] = true
            
            set pList[0] = 1
            set pList[1] = 7
            set pList[2] = 11
            set pList[3] = 13
            set pList[4] = 17
            set pList[5] = 19
            set pList[6] = 23
            set pList[7] = 29
        endmethod
    endmodule
    private struct Inits extends array
        implement Init
    endstruct

endlibrary

Feel free to comment on how silly this is.
 
^That would be expensive and could hit the op limit /easily/ with a value like 100 or 150 :/
A loop from 1 to 100 that has a loop from 1 to SquareRoot(MAX) in it where SquareRoot(MAX) is precomputed.
I'd have to make lots of trigger evals and make this a mess.

As for making an array of a couple thousand primes, it might help the brute force search.

My algorithm does well for most numbers.

60% of the time, the number is going to be handled in O(1) because it's going to end with either 0, 2, 4, 5, 6 or 8.

This leaves us with numbers that end with 1, 3, 7 and 9.

Sometimes, you'll have a number ending with these digits divisible by 3, so that would cancel out a reasonable chunk of numbers as well, leaving us with a relatively smaller set of numbers that have to be handled via the O(n) search.

Since all primes are of the form 30k + i where i is in the set {1, 7, 11, 13, 17, 19, 23, 29}, we have to check if it can be represented by any of those where k is a valid integer.

If it can't be represented by any of those, then it's not prime. (73% of the time, you have a number that can't be represented by this.)

If it can be represented, then it might be prime, so we have to bother with the O(n) search that goes from 7 to SquareRoot(n).

I'll optimize it when I have time.
 
Well, it would make sense, yes, but I hardcoded the check for primes in the IsPrime function so that it would just return a boolean value from the array if the number is less than 100 either way.

edit
Should I graveyard this?
I mean, IsPrime? Really? This is not a first-year programming class assignment submission section, this is a Warcraft III JASS script section.
Thoughts?
 
Top