Warcraft III resources & community, 2003–2006 · archived

IsPrimeNumber

not rated
Submitted by KaTTaNaCalculations0 downloads
Returns true if the given integer is a prime number.
I can imagine that it will be useful for advanced encryption methods.

Source

function IsPrimeNumber takes integer i returns boolean
    local integer p = 2
    local integer max = i
    local integer a = 1
    set i = IAbsBJ(i)
    if ( i == 1 ) then
        return false
    endif
    loop
        exitwhen (p >= max)
        if ( i/p + 0.0 == (i+0.0)/p ) then 
            return false
        endif
        set max = i/p
        set p = p + a
        set a = 2
    endloop
    return true
endfunction

Comments

1
This works perfectly fine, since most checked primes would probably be fairly small. It does check every odd number, but it would probably lose efficiency by trying to store primes it found along the way.