🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of anton19870803

anton19870803's solution

to Nth Prime in the Lua Track

Published at Nov 12 2019 · 0 comments
Test suite

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.


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.


local nth = require('nth-prime')

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

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

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

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

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

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

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

  it('should raise an error for n <= 0', function()

--- Get first n prime numbers.
-- Algorithm is based on the Sieve of Eratosthenes
-- (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes).
-- For this algorithm we should know number of `N` (number of
-- integers to check) which we don't know in advance. So we 
-- take a guess `N = 4 * n`. The algorithm is implemented
-- in 2 steps:
--  1. Do basic Sieve of Eratosthenes algorithm. In case we recieve the
--  desired number of primes just return them as a result. Otherwise
--  do step 2.
--  2. Increase `N` by 2, do some recalculations to mark some numbers
--  from range [old_N; new_N] as not primes and repeat step 1 for 
--  the range [old_N; new_N].
local function get_primes(n)
	local primes = {}
	local is_prime = {[1] = false}
	local N = n * 4 
	for i = 2, N do is_prime[i] = true end

	while true do
		-- Step 1. The Sieve of Eratosthenes.
		local last_prime = (#primes > 0) and primes[#primes] or 1
		for i = last_prime + 1, N do
			if is_prime[i] then 
				table.insert(primes, i)
				if #primes >= n then return primes end

				for k = i, (N/i) do
					is_prime[i * k] = false

		-- Step 2. Size of the sieve was to small. Increase
		-- sizes of the tables and then go to step 1.
		for i = N, N * 2 do is_prime[i] = true end
		local old_N = N
		N = N * 2
		for i = 1, #primes do
			local p = primes[i]
			for j = math.floor(old_N / p), (N / p) do
				is_prime[p * j] = false
	return primes

return function(n)
	assert(type(n) == 'number' and n > 0, "argument should be positive intener")
	local primes = get_primes(n)
	return primes[n]

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?