ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# stevenjohnstone's solution

## to Nth Prime in the Lua Track

Published at Jul 26 2020 · 0 comments
Instructions
Test suite
Solution

Given a number n, determine what the nth prime is.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

## Source

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### nth-prime_spec.lua

``````local nth = require('nth-prime')

describe('nth-prime', function()
local function benchmark(f)
local start = os.clock()
f()
return os.clock() - start
end

it('should give 2 as the first prime', function()
assert.equal(2, nth(1))
end)

it('should give 3 as the second prime', function()
assert.equal(3, nth(2))
end)

it('should be able to calculate the nth prime for small n', function()
assert.equal(13, nth(6))
end)

it('should be able to calculate the nth prime for large n', function()
assert.equal(104743, nth(10001))
end)

it('should be efficient for large n', function()
local execution_time = benchmark(function()
nth(10001)
end)

assert(execution_time < 1, 'should take less than a second to execute')
end)

it('should raise an error for n <= 0', function()
assert.has_error(function()
nth(0)
end)

assert.has_error(function()
nth(-1)
end)
end)
end)``````
``````local function mulmod(a, e, m)
local result = 1
while e > 0 do
if e % 2 == 1 then
result = result * a % m
e = e - 1
end
e = e / 2
a = a * a % m
end
return result
end

-- express n - 1 = 2^s * q for some odd q
local function sq(n)
local q = n - 1
local s = 0
while q % 2 == 0 do
s = s + 1
q = q // 2
end
return s, q
end

local function is_prime(p)
for f = 3, math.sqrt(p) do
if p % f == 0 then
return false
end
end
return true
end

local function miller_rabin(n)
local check = function()
local a = math.random(2, n -1)
local s, q = sq(n)
local m = mulmod(a, q, n)
if m == 1 or m == n - 1 then
return true
end

for _ = 1, s - 1 do
m = mulmod(m, 2, n)
if m == n - 1 then
return true
end
end
return false
end

for _ = 1, 4 do
if not check() then
return false
end
end
return is_prime(n)
end

local first_primes = {2, 3, 5, 7, 11, 13}
return function(n)
assert(n > 0)
local small_prime = first_primes[n]
if small_prime ~= nil then
return small_prime
end
local p, i = first_primes[#first_primes], #first_primes + 1
while true do
p = p + 2
if miller_rabin(p) then
if i == n then
return p
end
i = i + 1
end
end
end``````