-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a67a97
commit 70b7ddd
Showing
9 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters