Skip to content

Commit

Permalink
Solve p4, p34, p76 in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 9, 2024
1 parent deccd48 commit b928d7b
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
Expand Down
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down Expand Up @@ -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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down Expand Up @@ -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`| |
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
13 changes: 13 additions & 0 deletions docs/src/lua/lib/math.rst
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 1 addition & 1 deletion docs/src/lua/lib/range.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions docs/src/lua/p0004.rst
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions docs/src/lua/p0034.rst
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions docs/src/lua/p0076.rst
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
16 changes: 16 additions & 0 deletions lua/src/lib/math.lua
Original file line number Diff line number Diff line change
@@ -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,
}
30 changes: 30 additions & 0 deletions lua/src/p0004.lua
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion lua/src/p0028.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,3 +61,5 @@ return {
return answer
end
}

local range_entry = loadfile("src/lib/range.lua")().range_entry3
40 changes: 40 additions & 0 deletions lua/src/p0034.lua
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions lua/src/p0076.lua
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

Expand Down

0 comments on commit b928d7b

Please sign in to comment.