-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp7.lua
More file actions
40 lines (37 loc) · 1.05 KB
/
p7.lua
File metadata and controls
40 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env lua
--[[
What is the 10,001st prime number?
]]
--- Compute the nth smallest prime number.
---@param n number The index of the prime to compute.
local function get_nth_prime(n)
-- The first prime is 2
if n == 1 then
return 2
end
-- If n > 1, then iteratively compute prime numbers
local primes = {2}
local i = 3
while #primes < n do
-- Determine if i is prime by comparing against smaller primes
local is_i_prime = true
for j = 1, #primes do
-- Can ignore potential factors greater than ceil(sqrt(i))
if primes[j] > math.ceil(math.sqrt(i)) then
break
end
-- If i is divisible by a smaller prime, then it is not prime
if i % primes[j] == 0 then
is_i_prime = false
break
end
end
if is_i_prime then
table.insert(primes, i)
end
i = i + 2
end
return primes[#primes]
end
-- Compute solution
print(get_nth_prime(10001)) -- 104,743