Lua Implementation of Problem 5
View source code here on GitHub!
Includes
Solution
- solution()
- Returns:
The solution to problem 5
- Return type:
number
1-- Project Euler Problem 5
2--
3-- Problem:
4--
5
6local prime_factors = loadlib("primes").prime_factors
7
8return {
9 solution = function()
10 local answer = 1
11 local factorTracker = {}
12 local localFactorTracker = {}
13 for i = 1,20 do
14 factorTracker[i] = 0
15 localFactorTracker[i] = 0
16 end
17
18 for i = 2,20 do
19 local pf = prime_factors(i)
20 local p = pf.next()
21 repeat
22 localFactorTracker[p] = localFactorTracker[p] + 1
23 p = pf.next()
24 until not p
25
26 for j = 2,19 do
27 if factorTracker[j] < localFactorTracker[j] then
28 factorTracker[j] = localFactorTracker[j]
29 end
30 localFactorTracker[j] = 0
31 end
32 end
33 for i = 2,19 do
34 for _ = 1,factorTracker[i] do
35 answer = answer * i
36 end
37 end
38
39 return answer
40 end
41}