# Attempt to call a boolean value

My Lua-Function to do the nthPrime exercise runs perfectly on my compiler. But when i try to get it verified in the exercism editor, I get this error message:

``````./nth-prime_spec.lua:11: attempt to call a boolean value (upvalue 'nth')
``````

What is wrong?

This is my function:

``````local function nthPrime(n)
if n < 1 or n%1 ~= 0 then
return "error"
end
local testPrime = 1
local currentNumber = 0
local currentPrime = 0
while true do
testPrime = testPrime + 1
for i=2, testPrime, 1 do
if i == testPrime then
currentNumber = currentNumber + 1
currentPrime = testPrime
break
elseif testPrime%i == 0 then
break
end
end
if n == currentNumber then
return currentPrime
end
end
end
``````
1 Like

The stub looks like this:

``````return function(n)

end
``````

Maybe the first line needs to match?

``````return function(n)
...
end
``````

outputs:

``````Your tests timed out. ...
``````

You have to see how the test file uses your code

``````local nth = require('nth-prime')
...
assert.equal(2, nth(1))
``````

You need to ensure your module returns `nthPrime`

May you please explain a bit more? My function takes in a value ‘n’ and gives the nth-prime of it out. I don’t understand what the error message means. Where is it calling a boolean value? What exactly do I need to change to get the output accepted?

The test file expects your module to return a value. That value is a function (that the test file names `nth`). What do you need to do in `nth-prime.lua` to return a function?

Look carefully at the error message

``````./nth-prime_spec.lua:11: attempt to call a boolean value ...
``````

The error is coming from line 11 of the test suite. That will answer “Where is it calling a boolean value?”

If your module does not return a value, then `local nth = require('nth-prime')` assigns `true` to `nth`.

Thank you for clarification, but my function returns a value. If I enter for n=1, I get 2 as function output. Isn’t this what the function should do?

If this is not your first Lua exercise, check some of your other solutions. What gets returned from those modules?

Your module needs to return a function and that function needs to return an int.

Thanks, finally I got it. Every time I tried to it properly by returning a function, I got a timed out error with this for loop:

``````for i=2, n, 1 do
if n%i == 0 then
return false
end
end
``````

After including the fact that the for loop only needs to run up to the square root of n, the timed out error vanished and everything was working:

``````for i=2, math.sqrt(n), 1 do
if n%i == 0 then
return false
end
end
``````

So, I guess the exercism Lua compiler is way slower than my compiler and that is why i wasn’t able to find out what’s wrong.

There’s another optimization to be made: the loop `for i=2, math.sqrt(n), 1` has twice as many iterations as necessary. You don’t need to test i = 4, 6, 8, … because even numbers (except 2) cannot be prime.

``````if n%2 == 0 then return false end
for i=3, math.sqrt(n), 2 do
if n%i == 0 then
return false
end
end
``````

I thought about it, but isn’t this making it more complicated? The for loop starting with i=2 would check for an even number in the first run anyway.

I posted the code, you can decide how much more complicated it is. It does use 50% fewer loop iterations so it will be correspondingly faster

1 Like