Primes and primality checking

Devin Asay devin_asay at byu.edu
Tue Apr 25 11:01:27 EDT 2017


Hey folks,

This guy over on stackoverflow.com<http://stackoverflow.com> is asking for help generating and testing very large prime numbers.
http://stackoverflow.com/questions/43600252/generate-and-check-the-primality-of-very-large-numbers-in-livecode

I came up with this:

on mouseUp
    local tNum
    put fld "testNum" into tNum
    put isPrime(tNum) into fld "report"
end mouseUp

function isPrime pNum
    if pNum <> 2 AND pNum mod 2 = 0 then
        return false
    end if
    repeat with x = 3 to sqrt(pNum) step 2
        if pNum mod x = 0 then
            return false
        end if
    end repeat
    return true
end isPrime


However, it doesn’t seem to be reliable for very large numbers (> 100 digits) as the fellow wants. My math skills are pretty creaky. Anybody want a crack at this? It’s a fun challenge, plus it can boost LC’s presence on stackoverflow.

Cheers,

Devin

Devin Asay
Director
Office of Digital Humanities
Brigham Young University



More information about the use-livecode mailing list