From b928d7bf2700866837bb947816daa7edf81ee63b Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Sun, 8 Sep 2024 23:58:22 -0500 Subject: [PATCH] Solve p4, p34, p76 in lua --- README.rst | 2 +- docs/index.rst | 6 ++-- docs/src/lua/lib/math.rst | 13 ++++++++ docs/src/lua/lib/range.rst | 2 +- docs/src/lua/p0004.rst | 18 +++++++++++ docs/src/lua/p0034.rst | 23 ++++++++++++++ docs/src/lua/p0076.rst | 18 +++++++++++ lua/README.rst | 3 ++ lua/src/lib/math.lua | 16 ++++++++++ lua/src/p0004.lua | 30 ++++++++++++++++++ lua/src/p0028.lua | 3 +- lua/src/p0034.lua | 40 ++++++++++++++++++++++++ lua/src/p0076.lua | 62 ++++++++++++++++++++++++++++++++++++++ lua/test.lua | 3 ++ 14 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 docs/src/lua/lib/math.rst create mode 100644 docs/src/lua/p0004.rst create mode 100644 docs/src/lua/p0034.rst create mode 100644 docs/src/lua/p0076.rst create mode 100644 lua/src/lib/math.lua create mode 100644 lua/src/p0004.lua create mode 100644 lua/src/p0034.lua create mode 100644 lua/src/p0076.lua diff --git a/README.rst b/README.rst index a9380a6f..80551d9b 100644 --- a/README.rst +++ b/README.rst @@ -93,7 +93,7 @@ Olivia's Project Euler Solutions | | Browser [#]_ | | |CodeQL| |br| | | | | | |ESLint| | +------------+----------------------------+--------+-------------------+ - | Lua | Lua 5+ [#]_ | 7 | |Luai| |br| | + | Lua | Lua 5+ [#]_ | 10 | |Luai| |br| | | | | | |LuaCheck| | +------------+----------------------------+--------+-------------------+ | Python | CPython 3.6+ |br| | 80 | |Python| |br| | diff --git a/docs/index.rst b/docs/index.rst index ab7e9ba2..aa4a2a17 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -83,7 +83,7 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`003`|:c-d:`0003`|:cp-d:`0003`|:cs-d:`0003`| |:js-d:`0003`| |:py-d:`0003`|:rs-d:`0003`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ - |:prob:`004`|:c-d:`0004`|:cp-d:`0004`|:cs-d:`0004`|:ja-d:`0004`|:js-d:`0004`| |:py-d:`0004`|:rs-d:`0004`| + |: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`| | | |:js-d:`0005`| |:py-d:`0005`|:rs-d:`0005`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -141,7 +141,7 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`033`| | | | | | |:py-d:`0033`| | +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ - |:prob:`034`|:c-d:`0034`|:cp-d:`0034`|:cs-d:`0034`|:ja-d:`0034`|:js-d:`0034`| |:py-d:`0034`|:rs-d:`0034`| + |:prob:`034`|:c-d:`0034`|:cp-d:`0034`|:cs-d:`0034`|:ja-d:`0034`|:js-d:`0034`|:lu-d:`0034`|:py-d:`0034`|:rs-d:`0034`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`035`| | | | |:js-d:`0035`| |:py-d:`0035`|:rs-s:`0035`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -205,7 +205,7 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`074`| | | | | | |:py-d:`0074`| | +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ - |:prob:`076`|:c-d:`0076`|:cp-d:`0076`|:cs-s:`0076`|:ja-d:`0076`|:js-s:`0076`| |:py-d:`0076`|:rs-s:`0076`| + |:prob:`076`|:c-d:`0076`|:cp-d:`0076`|:cs-s:`0076`|:ja-d:`0076`|:js-s:`0076`|:lu-d:`0076`|:py-d:`0076`|:rs-s:`0076`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`077`| | | | | | |:py-d:`0077`| | +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/lua/lib/math.rst b/docs/src/lua/lib/math.rst new file mode 100644 index 00000000..d96661ba --- /dev/null +++ b/docs/src/lua/lib/math.rst @@ -0,0 +1,13 @@ +math.lua +======== + +View source code :source:`lua/src/lib/math.lua` + +.. lua:function:: factorial(n) + + :return: The factorial of n + :rtype: number + +.. literalinclude:: ../../../../lua/src/lib/math.lua + :language: Lua + :linenos: diff --git a/docs/src/lua/lib/range.rst b/docs/src/lua/lib/range.rst index 569b40ae..deb1adb1 100644 --- a/docs/src/lua/lib/range.rst +++ b/docs/src/lua/lib/range.rst @@ -3,7 +3,7 @@ range.lua View source code :source:`lua/src/lib/range.lua` -This module directly ports some of the logic found in Python's :external:py:func:`range`. +This module directly ports some of the logic found in Python's :external:py:obj:`range`. .. lua:function:: range_entry3(start, step, idx) diff --git a/docs/src/lua/p0004.rst b/docs/src/lua/p0004.rst new file mode 100644 index 00000000..23f24223 --- /dev/null +++ b/docs/src/lua/p0004.rst @@ -0,0 +1,18 @@ +Lua Implementation of Problem 4 +=============================== + +View source code :source:`lua/src/p0004.lua` + +Solution +-------- + +.. lua:function:: solution() + + :return: The solution to problem 4 + :rtype: number + +.. literalinclude:: ../../../lua/src/p0004.lua + :language: Lua + :linenos: + +.. tags:: palindrome diff --git a/docs/src/lua/p0034.rst b/docs/src/lua/p0034.rst new file mode 100644 index 00000000..2ecc7520 --- /dev/null +++ b/docs/src/lua/p0034.rst @@ -0,0 +1,23 @@ +Lua Implementation of Problem 34 +================================ + +View source code :source:`lua/src/p0034.lua` + +Includes +-------- + +- `math.lua <./lib/math.html>`__ + +Solution +-------- + +.. lua:function:: solution() + + :return: The solution to problem 34 + :rtype: number + +.. literalinclude:: ../../../lua/src/p0034.lua + :language: Lua + :linenos: + +.. tags:: factorial, digit-sum diff --git a/docs/src/lua/p0076.rst b/docs/src/lua/p0076.rst new file mode 100644 index 00000000..5aad9658 --- /dev/null +++ b/docs/src/lua/p0076.rst @@ -0,0 +1,18 @@ +Lua Implementation of Problem 76 +================================ + +View source code :source:`lua/src/p0076.lua` + +Solution +-------- + +.. lua:function:: solution() + + :return: The solution to problem 76 + :rtype: number + +.. literalinclude:: ../../../lua/src/p0076.lua + :language: Lua + :linenos: + +.. tags:: partition diff --git a/lua/README.rst b/lua/README.rst index 7ad542ea..5a05e85e 100644 --- a/lua/README.rst +++ b/lua/README.rst @@ -57,8 +57,11 @@ Problems Solved - ☒ `1 <./src/p0001.lua>`__ - ☒ `2 <./src/p0002.lua>`__ +- ☒ `4 <./src/p0004.lua>`__ - ☒ `6 <./src/p0006.lua>`__ - ☒ `9 <./src/p0009.lua>`__ - ☒ `17 <./src/p0017.lua>`__ - ☒ `28 <./src/p0028.lua>`__ +- ☒ `34 <./src/p0034.lua>`__ +- ☒ `76 <./src/p0076.lua>`__ - ☒ `836 <./src/p0836.lua>`__ diff --git a/lua/src/lib/math.lua b/lua/src/lib/math.lua new file mode 100644 index 00000000..44fb1264 --- /dev/null +++ b/lua/src/lib/math.lua @@ -0,0 +1,16 @@ +-- This file is a direct port of Python's range functions + +local function factorial(n) + local answer = 1 + + for i = 2,n,1 + do + answer = answer * i + end + + return answer +end + +return { + factorial = factorial, +} diff --git a/lua/src/p0004.lua b/lua/src/p0004.lua new file mode 100644 index 00000000..3dc977a9 --- /dev/null +++ b/lua/src/p0004.lua @@ -0,0 +1,30 @@ +-- Project Euler Problem 4 +-- +-- Problem: +-- +-- A palindromic number reads the same both ways. The largest palindrome made from +-- the product of two 2-digit numbers is 9009 = 91 × 99. +-- +-- Find the largest palindrome made from the product of two 3-digit numbers. + +return { + solution = function() + local answer = 0 + + for v = 101,1001,1 + do + for u = 100,(v-1),1 + do + local p = u * v + local ps = tostring(p) + + if ps == ps.reverse() and p > answer + then + answer = p + end + end + end + + return answer + end +} diff --git a/lua/src/p0028.lua b/lua/src/p0028.lua index 079a1b45..1eb4007b 100644 --- a/lua/src/p0028.lua +++ b/lua/src/p0028.lua @@ -48,7 +48,6 @@ return { solution = function() local answer = 1 - local range_entry = loadfile("src/lib/range.lua")().range_entry3 for i = 1,(1000 / 2),1 do @@ -62,3 +61,5 @@ return { return answer end } + +local range_entry = loadfile("src/lib/range.lua")().range_entry3 diff --git a/lua/src/p0034.lua b/lua/src/p0034.lua new file mode 100644 index 00000000..359b6b5c --- /dev/null +++ b/lua/src/p0034.lua @@ -0,0 +1,40 @@ +-- Project Euler Problem 34 +-- +-- This ended up being a filtering problem. The problem with my solution is that I +-- am not satisfied with my filter at all. I feel like there is a more efficient +-- way to go about it. +-- +-- Problem: +-- +-- 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. +-- +-- Find the sum of all numbers which are equal to the sum of the factorial of +-- their digits. +-- +-- Note: as 1! = 1 and 2! = 2 are not sums they are not included. + +return { + solution = function() + local answer = 0 + + for x = 10,99999,1 + do + local xs = tostring(x) + local sum = 0 + + for i = 1,xs.len(),1 + do + sum = sum + factorial(tonumber(xs[i])) + end + + if sum == x + do + answer = answer + 1 + end + end + + return answer + end +} + +local factorial = loadfile('src/lib/math.lua')().factorial diff --git a/lua/src/p0076.lua b/lua/src/p0076.lua new file mode 100644 index 00000000..53bec302 --- /dev/null +++ b/lua/src/p0076.lua @@ -0,0 +1,62 @@ +-- Project Euler Problem 34 +-- +-- This ended up being a filtering problem. The problem with my solution is that I +-- am not satisfied with my filter at all. I feel like there is a more efficient +-- way to go about it. +-- +-- Problem: +-- +-- 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. +-- +-- Find the sum of all numbers which are equal to the sum of the factorial of +-- their digits. +-- +-- Note: as 1! = 1 and 2! = 2 are not sums they are not included. + +return { + solution = function() + local answer = 0 + local idx = 0 + local sum = 100 + local counts = {} + + for i = 1, 101 do + counts[i] = 0 + end + + counts[2 + 1] = 0 + + while counts[100 + 1] == 0 + do + counts[2 + 1] = counts[2 + 1] + 2 + + if sum >= 100 + then + answer = answer + (100 + counts[2 + 1] - sum) / 2 + idx = 2 + + repeat + idx = idx + 1 + counts[idx] = 0 -- please remember lua is 1-indexed + idx = idx + 1 + counts[idx] = counts[idx] + idx - 1 + + for i = (idx - 1),101,1 + do + sum = sum + counts[i] + end + until sum <= 100 + end + + sum = 0 + for i = 1,101,1 + do + sum = sum + counts[i] + end + end + + return answer + end +} + +local factorial = loadfile('src/lib/math.lua')().factorial diff --git a/lua/test.lua b/lua/test.lua index cc432cb3..2c85b20e 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -46,10 +46,13 @@ end local problems = { ["p0001.lua"] = {233168, 60}, ["p0002.lua"] = {4613732, 60}, + ["p0004.lua"] = {906609, 60}, ["p0006.lua"] = {25164150, 60}, ["p0009.lua"] = {31875000, 60}, ["p0017.lua"] = {21124, 60}, ["p0028.lua"] = {669171001, 60}, + ["p0034.lua"] = {40730, 60}, + ["p0076.lua"] = {190569291, 60}, ["p0836.lua"] = {"aprilfoolsjoke", 60}, }