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

ketigid'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)
function checker()
    local checklist = {}
    -- Append item to the table at index
    checklist.append = function(self, index, item)
        if self[index] == nil then
            self[index] = {item}
        else
            local value = self[index]
            value[#value + 1] = item
        end
    end
    -- Seed a new prime
    checklist.seed = function(self, index)
        self:append(index, index)
    end
    -- Propagate the list of primes at index
    checklist.propagate = function(self, index)
        for _, number in pairs(self[index]) do
            self:append(index + number, number)
        end
    end

    return checklist
end

return function(n)
    assert(n > 0, "n should be positive")

    local checklist = checker()

    local count = 2
    local primes = {}
    while #primes < n do
        if checklist[count] == nil then
            -- Include as prime
            primes[#primes + 1] = count
            -- Seed prime into checklist
            checklist:seed(count)
        end
        -- Propagate the primes at this count
        checklist:propagate(count)
        count = count + 1
    end

    return primes[#primes]
end

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?