Skip to content

Commit

Permalink
solve p5 in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 17, 2024
1 parent e167c33 commit 7fdef1d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Olivia's Project Euler Solutions
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
+------------+----------------------------+--------+-------------------+
| Lua | PUC-Rio Lua 5+ [#]_ | 17 | |Luai| |br| |
| Lua | PUC-Rio Lua 5+ [#]_ | 18 | |Luai| |br| |
| | | | |Lu-Cov| |br| |
| | | | |LuaCheck| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`004`|:c-d:`0004`|:cp-d:`0004`|:cs-d:`0004`|:ja-d:`0004`|:js-d:`0004`|:lu-d:`0004`|:py-d:`0004`|:rs-d:`0004`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`005`|:c-d:`0005`|:cp-d:`0005`|:cs-d:`0005`|:ja-d:`0005`|:js-d:`0005`| |:py-d:`0005`|:rs-d:`0005`|
|:prob:`005`|:c-d:`0005`|:cp-d:`0005`|:cs-d:`0005`|:ja-d:`0005`|:js-d:`0005`|:lu-d:`0005`|:py-d:`0005`|:rs-d:`0005`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`006`|:c-d:`0006`|:cp-d:`0006`|:cs-d:`0006`|:ja-d:`0006`|:js-d:`0006`|:lu-d:`0006`|:py-d:`0006`|:rs-d:`0006`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
23 changes: 23 additions & 0 deletions docs/src/lua/p0005.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Lua Implementation of Problem 5
===============================

View source code :source:`lua/src/p0005.lua`

Includes
--------

- `primes.lua <./lib/primes.html>`__

Solution
--------

.. lua:function:: solution()
:return: The solution to problem 5
:rtype: number

.. literalinclude:: ../../../lua/src/p0005.lua
:language: Lua
:linenos:

.. tags:: factorization, prime-number, lua-iterator
1 change: 1 addition & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Problems Solved
- ☒ `2 <./src/p0002.lua>`__
- ☒ `3 <./src/p0003.lua>`__
- ☒ `4 <./src/p0004.lua>`__
- ☒ `5 <./src/p0005.lua>`__
- ☒ `6 <./src/p0006.lua>`__
- ☒ `7 <./src/p0007.lua>`__
- ☒ `8 <./src/p0008.lua>`__
Expand Down
41 changes: 41 additions & 0 deletions lua/src/p0005.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Project Euler Problem 5
--
-- Problem:
--

local prime_factors = loadlib("primes").prime_factors

return {
solution = function()
local answer = 1
local factorTracker = {}
local localFactorTracker = {}
for i = 1,20 do
factorTracker[i] = 0
localFactorTracker[i] = 0
end

for i = 2,20 do
local pf = prime_factors(i)
local p = pf.next()
repeat
localFactorTracker[p] = localFactorTracker[p] + 1
p = pf.next()
until not p

for j = 2,19 do
if factorTracker[j] < localFactorTracker[j] then
factorTracker[j] = localFactorTracker[j]
end
localFactorTracker[j] = 0
end
end
for i = 2,19 do
for j = 1,factorTracker[i] do
answer = answer * i
end
end

return answer
end
}
1 change: 1 addition & 0 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ local problems = {
["p0002.lua"] = {4613732, false},
["p0003.lua"] = {6857, false},
["p0004.lua"] = {906609, false},
["p0005.lua"] = {232792560, false},
["p0006.lua"] = {25164150, false},
["p0007.lua"] = {104743, false},
["p0008.lua"] = {23514624000, false},
Expand Down

0 comments on commit 7fdef1d

Please sign in to comment.