Skip to content

Commit

Permalink
Attempt p3 in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 10, 2024
1 parent 0a67a97 commit 70b7ddd
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 4 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+ [#]_ | 11 | |Luai| |br| |
| Lua | Lua 5+ [#]_ | 12 | |Luai| |br| |
| | | | |LuaCheck| |
+------------+----------------------------+--------+-------------------+
| Python | CPython 3.6+ |br| | 80 | |Python| |br| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
20 changes: 20 additions & 0 deletions docs/src/lua/lib/primes.rst
Original file line number Diff line number Diff line change
@@ -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:
23 changes: 23 additions & 0 deletions docs/src/lua/p0003.rst
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand Down
2 changes: 0 additions & 2 deletions lua/src/lib/math.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
-- This file is a direct port of Python's range functions

local function factorial(n)
local answer = 1

Expand Down
66 changes: 66 additions & 0 deletions lua/src/lib/primes.lua
Original file line number Diff line number Diff line change
@@ -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,
}
22 changes: 22 additions & 0 deletions lua/src/p0003.lua
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 70b7ddd

Please sign in to comment.