Sha256: 5c7f0b86a508cb780ac64de7e523b5c531f0fc32190277616dbb16cefa55e3a2

Contents?: true

Size: 393 Bytes

Versions: 396

Compression:

Stored size: 393 Bytes

Contents

return function(n)
  assert(n > 0, 'n must be positive')

  local primes = {}

  local function is_prime(n)
    for _, p in ipairs(primes) do
      if n % p == 0 then return false end
      if p^2 > n then return true end
    end
    return true
  end

  for i = 2, math.huge do
    if is_prime(i) then table.insert(primes, i) end
    if #primes == n then return primes[#primes] end
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.179 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.178 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.177 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.176 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.175 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.174 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.173 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.172 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.171 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.170 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.169 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.167 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.166 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.165 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.164 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.163 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.162 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.161 tracks/lua/exercises/nth-prime/example.lua
trackler-2.2.1.160 tracks/lua/exercises/nth-prime/example.lua