From 5260a856667fc5d416f81578f3eec1549a17998c Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Tue, 10 Sep 2024 22:02:32 -0500 Subject: [PATCH] solve p7 in lua, fix p3. primes() is too slow --- docs/index.rst | 4 ++-- docs/src/lua/p0007.rst | 23 +++++++++++++++++++++++ lua/README.rst | 1 + lua/src/lib/primes.lua | 41 +++++++++++++++++++++++++++++++---------- lua/src/p0003.lua | 7 +++++-- lua/src/p0007.lua | 22 ++++++++++++++++++++++ lua/test.lua | 30 +++++++++++++++++++++++++++++- 7 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 docs/src/lua/p0007.rst create mode 100644 lua/src/p0007.lua diff --git a/docs/index.rst b/docs/index.rst index e85e71bd..7140c0b8 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`|:lu-d:`0003`|:py-d:`0003`|:rs-d:`0003`| + |:prob:`003`|:c-d:`0003`|:cp-d:`0003`|:cs-d:`0003`| |:js-d:`0003`|:lu-s:`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`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ @@ -89,7 +89,7 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |: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`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ - |:prob:`007`|:c-d:`0007`|:cp-d:`0007`|:cs-d:`0007`| |:js-d:`0007`| |:py-d:`0007`|:rs-d:`0007`| + |:prob:`007`|:c-d:`0007`|:cp-d:`0007`|:cs-d:`0007`| |:js-d:`0007`|:lu-s:`0003`|:py-d:`0007`|:rs-d:`0007`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`008`|:c-d:`0008`|:cp-d:`0008`|:cs-d:`0008`|:ja-d:`0008`|:js-d:`0008`|:lu-d:`0008`|:py-d:`0008`|:rs-d:`0008`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/src/lua/p0007.rst b/docs/src/lua/p0007.rst new file mode 100644 index 00000000..c924503c --- /dev/null +++ b/docs/src/lua/p0007.rst @@ -0,0 +1,23 @@ +Lua Implementation of Problem 7 +=============================== + +View source code :source:`lua/src/p0007.lua` + +Includes +-------- + +- `primes.lua <./lib/primes.html>`__ + +Solution +-------- + +.. lua:function:: solution() + + :return: The solution to problem 7 + :rtype: number + +.. literalinclude:: ../../../lua/src/p0007.lua + :language: Lua + :linenos: + +.. tags:: factorization, prime-number, lua-iterator diff --git a/lua/README.rst b/lua/README.rst index 79bc2b40..d0d8d3e6 100644 --- a/lua/README.rst +++ b/lua/README.rst @@ -60,6 +60,7 @@ Problems Solved - ☒ `3 <./src/p0003.lua>`__ - ☒ `4 <./src/p0004.lua>`__ - ☒ `6 <./src/p0006.lua>`__ +- ☒ `7 <./src/p0007.lua>`__ - ☒ `8 <./src/p0008.lua>`__ - ☒ `9 <./src/p0009.lua>`__ - ☒ `17 <./src/p0017.lua>`__ diff --git a/lua/src/lib/primes.lua b/lua/src/lib/primes.lua index 6930da8f..0990532c 100644 --- a/lua/src/lib/primes.lua +++ b/lua/src/lib/primes.lua @@ -3,11 +3,16 @@ local function primes(stop) local known_primes = {} local function next() - if p == 2 - do - p = 3 - known_primes[0] = 2 - return 2 + if p < 7 + then + local ret = p + local transform = { + [2] = 3, + [3] = 5, + [5] = 7, + } + p = transform[p] + return ret end while stop == nil or p < stop @@ -15,18 +20,26 @@ local function primes(stop) local broken = false for i = 1,#known_primes,1 do - if p % known_primes[0] ~= 0 + if p % known_primes[i] ~= 0 then broken = true break end end - p = p + 2 + local ret = p + + if p % 6 == 1 + then + p = p + 4 + else + p = p + 2 + end + if not broken then - known_primes[#known_primes + 1] = p - 2 - return p - 2 + known_primes[#known_primes + 1] = ret + return ret end end end @@ -41,9 +54,11 @@ local function prime_factors(n) local p = pgen.next() local function next() - while p ~= nil and n % p != 0 + print(p .. ", " .. n) + while p ~= nil and n % p ~= 0 do p = pgen.next() + print(p) end if p == nil @@ -51,6 +66,12 @@ local function prime_factors(n) return nil end + if n < 0 + then + n = -n + return -1 + end + n = n / p return p end diff --git a/lua/src/p0003.lua b/lua/src/p0003.lua index 52e15181..cc9730e6 100644 --- a/lua/src/p0003.lua +++ b/lua/src/p0003.lua @@ -6,15 +6,18 @@ -- -- What is the largest prime factor of the number 600851475143 ? +local prime_factors = loadlib("primes").prime_factors + return { solution = function() local fgen = prime_factors(600851475143) - local f = 0 + local f = fgen.next() local answer = 0 - while (f = fgen.next()) ~= nil + while f ~= nil do answer = f + f = fgen.next() end return answer diff --git a/lua/src/p0007.lua b/lua/src/p0007.lua new file mode 100644 index 00000000..f81cb7a9 --- /dev/null +++ b/lua/src/p0007.lua @@ -0,0 +1,22 @@ +-- Project Euler Problem 7 +-- +-- Problem: +-- + +local primes = loadlib("primes").primes + +return { + solution = function() + local idx = 0 + local num = 0 + local pgen = primes() + + repeat + idx = idx + 1 + num = pgen.next() + print(idx .. ", " .. num) + until idx > 10000 + + return num + end +} diff --git a/lua/test.lua b/lua/test.lua index 3d2773a8..961cde73 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -1,3 +1,29 @@ +function loadlib(...) + local libname = select(1, ...) + local fnames = {} + local length = select("#", ...) + + local lib, err = loadfile("src/lib/" .. select(1, ...) .. ".lua") + if not lib + then + error("Failed to load lib " .. libname .. ": " .. err) + end + lib = lib() + + if length == 1 + then + return lib + end + + local ret = {} + for i = 2,length,1 + do + local fname = select(i, ...) + ret[fname] = lib[fname] + end + return ret +end + -- Function to load a problem solution file local function load_problem(file_name) local func, err = loadfile(file_name) @@ -17,6 +43,7 @@ end -- Timing and result check function local function check_problem(file_name, expected_answer, is_slow, problem_name) + print("Starting: " .. file_name) local problem_func = load_problem("src/" .. file_name) local start_time = os.clock() local success, result = pcall(problem_func) @@ -47,7 +74,6 @@ 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}, @@ -56,6 +82,8 @@ local problems = { ["p0028.lua"] = {669171001, false}, ["p0034.lua"] = {40730, false}, ["p0836.lua"] = {"aprilfoolsjoke", false}, + ["p0003.lua"] = {6857, true}, + ["p0007.lua"] = {104743, true}, ["p0076.lua"] = {190569291, true}, }