From 7fdef1dbe6b51d0106b46ccdee80e8c70c94b5b1 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Mon, 16 Sep 2024 22:09:22 -0500 Subject: [PATCH] solve p5 in lua --- README.rst | 2 +- docs/index.rst | 2 +- docs/src/lua/p0005.rst | 23 +++++++++++++++++++++++ lua/README.rst | 1 + lua/src/p0005.lua | 41 +++++++++++++++++++++++++++++++++++++++++ lua/test.lua | 1 + 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 docs/src/lua/p0005.rst create mode 100644 lua/src/p0005.lua diff --git a/README.rst b/README.rst index 10b846a4..ba757f9f 100644 --- a/README.rst +++ b/README.rst @@ -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| | +------------+----------------------------+--------+-------------------+ diff --git a/docs/index.rst b/docs/index.rst index 340fe718..798d93c5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/lua/p0005.rst b/docs/src/lua/p0005.rst new file mode 100644 index 00000000..5e26194e --- /dev/null +++ b/docs/src/lua/p0005.rst @@ -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 diff --git a/lua/README.rst b/lua/README.rst index cf01e5b9..988ae3af 100644 --- a/lua/README.rst +++ b/lua/README.rst @@ -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>`__ diff --git a/lua/src/p0005.lua b/lua/src/p0005.lua new file mode 100644 index 00000000..3073643d --- /dev/null +++ b/lua/src/p0005.lua @@ -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 +} diff --git a/lua/test.lua b/lua/test.lua index 353be544..bc4073f4 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -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},