From 70b7ddd7e4f805194543d8afa66906906d03cb5f Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Mon, 9 Sep 2024 23:40:19 -0500 Subject: [PATCH] Attempt p3 in lua --- README.rst | 2 +- docs/index.rst | 2 +- docs/src/lua/lib/primes.rst | 20 +++++++++++ docs/src/lua/p0003.rst | 23 +++++++++++++ lua/README.rst | 1 + lua/src/lib/math.lua | 2 -- lua/src/lib/primes.lua | 66 +++++++++++++++++++++++++++++++++++++ lua/src/p0003.lua | 22 +++++++++++++ lua/test.lua | 1 + 9 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 docs/src/lua/lib/primes.rst create mode 100644 docs/src/lua/p0003.rst create mode 100644 lua/src/lib/primes.lua create mode 100644 lua/src/p0003.lua diff --git a/README.rst b/README.rst index ca5eda02..6d7e5fbe 100644 --- a/README.rst +++ b/README.rst @@ -93,7 +93,7 @@ Olivia's Project Euler Solutions | | Browser [#]_ | | |CodeQL| |br| | | | | | |ESLint| | +------------+----------------------------+--------+-------------------+ - | Lua | Lua 5+ [#]_ | 11 | |Luai| |br| | + | Lua | Lua 5+ [#]_ | 12 | |Luai| |br| | | | | | |LuaCheck| | +------------+----------------------------+--------+-------------------+ | Python | CPython 3.6+ |br| | 80 | |Python| |br| | diff --git a/docs/index.rst b/docs/index.rst index 6fee236c..e85e71bd 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -81,7 +81,7 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`002`|:c-d:`0002`|:cp-d:`0002`|:cs-d:`0002`|:ja-d:`0002`|:js-d:`0002`|:lu-d:`0002`|:py-d:`0002`|:rs-d:`0002`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ - |:prob:`003`|:c-d:`0003`|:cp-d:`0003`|:cs-d:`0003`| |:js-d:`0003`| |:py-d:`0003`|:rs-d:`0003`| + |:prob:`003`|:c-d:`0003`|:cp-d:`0003`|:cs-d:`0003`| |:js-d:`0003`|:lu-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`|:lu-d:`0004`|:py-d:`0004`|:rs-d:`0004`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/lua/lib/primes.rst b/docs/src/lua/lib/primes.rst new file mode 100644 index 00000000..5f738309 --- /dev/null +++ b/docs/src/lua/lib/primes.rst @@ -0,0 +1,20 @@ +primes.lua +========= + +View source code :source:`lua/src/lib/primes.lua` + +This module directly ports some of the logic found in Python's :external:py:obj:`primes`. + +.. lua:function:: prime(stop) + + :return: An iterator over the prime numbers, optionally up to ``stop`` + :rtype: table + +.. lua:function:: primes_factors(n) + + :return: An iterator over the prime factors of ``n`` + :rtype: table + +.. literalinclude:: ../../../../lua/src/lib/primes.lua + :language: Lua + :linenos: diff --git a/docs/src/lua/p0003.rst b/docs/src/lua/p0003.rst new file mode 100644 index 00000000..2c5874cd --- /dev/null +++ b/docs/src/lua/p0003.rst @@ -0,0 +1,23 @@ +Lua Implementation of Problem 3 +=============================== + +View source code :source:`lua/src/p0003.lua` + +Includes +-------- + +- `primes.lua <./lib/primes.html>`__ + +Solution +-------- + +.. lua:function:: solution() + + :return: The solution to problem 3 + :rtype: number + +.. literalinclude:: ../../../lua/src/p0003.lua + :language: Lua + :linenos: + +.. tags:: factorization, prime-number, lua-iterator diff --git a/lua/README.rst b/lua/README.rst index 8a72f9b0..79bc2b40 100644 --- a/lua/README.rst +++ b/lua/README.rst @@ -57,6 +57,7 @@ Problems Solved - ☒ `1 <./src/p0001.lua>`__ - ☒ `2 <./src/p0002.lua>`__ +- ☒ `3 <./src/p0003.lua>`__ - ☒ `4 <./src/p0004.lua>`__ - ☒ `6 <./src/p0006.lua>`__ - ☒ `8 <./src/p0008.lua>`__ diff --git a/lua/src/lib/math.lua b/lua/src/lib/math.lua index 44fb1264..14a92688 100644 --- a/lua/src/lib/math.lua +++ b/lua/src/lib/math.lua @@ -1,5 +1,3 @@ --- This file is a direct port of Python's range functions - local function factorial(n) local answer = 1 diff --git a/lua/src/lib/primes.lua b/lua/src/lib/primes.lua new file mode 100644 index 00000000..6930da8f --- /dev/null +++ b/lua/src/lib/primes.lua @@ -0,0 +1,66 @@ +local function primes(stop) + local p = 2 + local known_primes = {} + + local function next() + if p == 2 + do + p = 3 + known_primes[0] = 2 + return 2 + end + + while stop == nil or p < stop + do + local broken = false + for i = 1,#known_primes,1 + do + if p % known_primes[0] ~= 0 + then + broken = true + break + end + end + + p = p + 2 + if not broken + then + known_primes[#known_primes + 1] = p - 2 + return p - 2 + end + end + end + + return { + next = next, + } +end + +local function prime_factors(n) + local pgen = primes() + local p = pgen.next() + + local function next() + while p ~= nil and n % p != 0 + do + p = pgen.next() + end + + if p == nil + then + return nil + end + + n = n / p + return p + end + + return { + next = next, + } +end + +return { + primes = primes, + prime_factors = prime_factors, +} diff --git a/lua/src/p0003.lua b/lua/src/p0003.lua new file mode 100644 index 00000000..52e15181 --- /dev/null +++ b/lua/src/p0003.lua @@ -0,0 +1,22 @@ +-- Project Euler Problem 3 +-- +-- Problem: +-- +-- The prime factors of 13195 are 5, 7, 13 and 29. +-- +-- What is the largest prime factor of the number 600851475143 ? + +return { + solution = function() + local fgen = prime_factors(600851475143) + local f = 0 + local answer = 0 + + while (f = fgen.next()) ~= nil + do + answer = f + end + + return answer + end +} diff --git a/lua/test.lua b/lua/test.lua index f0f0d299..3d2773a8 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -47,6 +47,7 @@ end local problems = { ["p0001.lua"] = {233168, false}, ["p0002.lua"] = {4613732, false}, + ["p0003.lua"] = {6857, false}, ["p0004.lua"] = {906609, false}, ["p0006.lua"] = {25164150, false}, ["p0008.lua"] = {23514624000, false},