Skip to content

Instantly share code, notes, and snippets.

@adam000
Created October 30, 2013 00:14
Show Gist options
  • Save adam000/7225066 to your computer and use it in GitHub Desktop.
Save adam000/7225066 to your computer and use it in GitHub Desktop.
A fun implementation of a prime finder using metatables
-- Metatable implementation of primes
primes = { 2, 3, 5, 7, 11, 13 }
__mt = {
__index = function(table, index)
local val = table[index - 1]
repeat
val = val + 2
local isPrime = true
for i = 2, index - 1 do
if val % table[i] == 0 then
isPrime = false
break
end
end
if isPrime then
break
end
until false
return val
end
}
setmetatable(primes, __mt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment