aboutsummaryrefslogtreecommitdiff
path: root/challenge-150/deadmarshal/lua/ch-2.lua
blob: c76007c02e541bc82bb5e69534a345d3371d2998 (plain)
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
local function prime_factors(n)
  assert(type(n) == "number", "n must be a number!")
  local t, c = {}, 2
  while n > 1 do
    if n % c == 0 then
      t[#t+1] = c
      n = n // c
    else c = c + 1
    end
  end
  return t
end

local function uniq(t)
  assert(type(t) == "table", "t must be a table!")
  table.sort(t)
  for i=1, #t do
    for j=i+1, #t do
      if t[i] == t[j] then return false end
    end
  end
  return true
end

local function square_free_integers()
  local t, i = {}, 1
  repeat
    if uniq(prime_factors(i)) then
      t[#t+1] = i
    end
    i = i + 1 
  until i >= 500
  return t
end

local sfi = square_free_integers()
for i=1, #sfi do io.write(sfi[i], ' ') end