-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprime-numbers.lua
33 lines (29 loc) · 924 Bytes
/
prime-numbers.lua
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
io.write("range: ")
local p = tonumber(io.read())
-- Initialize an array of boolean values representing primality, initially all set to "T" (True)
local primes = {}
for i = 1, p do
primes[i] = "T"
end
-- Iterate through each number in the range starting from 2 to p-1
for possiblePrime = 2, p - 1 do
-- If the number is still marked as prime
if primes[possiblePrime] == "T" then
-- Mark all multiples of the number as not prime ("F")
for multiple = possiblePrime * 2, p - 1, possiblePrime do
primes[multiple] = "F"
end
end
end
-- Create a list of prime numbers by filtering indices marked as "T" (True) and greater than 1
local primeNumbers = {}
for index = 2, p - 1 do
if primes[index] == "T" then
table.insert(primeNumbers, index)
end
end
-- Print the list of prime numbers
for _, prime in ipairs(primeNumbers) do
io.write(prime, " ")
end
print()