Skip to content

Commit

Permalink
solve p7 in lua, fix p3. primes() is too slow
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 11, 2024
1 parent 70b7ddd commit 5260a85
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ 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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`005`|:c-d:`0005`| | | |:js-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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|: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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
23 changes: 23 additions & 0 deletions docs/src/lua/p0007.rst
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand Down
41 changes: 31 additions & 10 deletions lua/src/lib/primes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,43 @@ 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
do
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
Expand All @@ -41,16 +54,24 @@ 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
then
return nil
end

if n < 0
then
n = -n
return -1
end

n = n / p
return p
end
Expand Down
7 changes: 5 additions & 2 deletions lua/src/p0003.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lua/src/p0007.lua
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 29 additions & 1 deletion lua/test.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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},
Expand All @@ -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},
}

Expand Down

0 comments on commit 5260a85

Please sign in to comment.