-
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
dc7d9c8
commit 7d33721
Showing
7 changed files
with
80 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,18 @@ | ||
Lua Implementation of Problem 14 | ||
================================ | ||
|
||
View source code :source:`lua/src/p0014.lua` | ||
|
||
Solution | ||
-------- | ||
|
||
.. lua:function:: solution() | ||
:return: The solution to problem 14 | ||
:rtype: number | ||
|
||
.. literalinclude:: ../../../lua/src/p0014.lua | ||
:language: Lua | ||
:linenos: | ||
|
||
.. tags:: collatz, recursion, longest-path |
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 |
---|---|---|
|
@@ -20,4 +20,4 @@ Solution | |
:language: Lua | ||
:linenos: | ||
|
||
.. tags:: large-numbers | ||
.. tags:: combinatorics |
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,55 @@ | ||
-- Project Euler Problem 14 | ||
-- | ||
-- Problem: | ||
-- | ||
-- The following iterative sequence is defined for the set of positive integers: | ||
-- | ||
-- n → n/2 (n is even) | ||
-- n → 3n + 1 (n is odd) | ||
-- | ||
-- Using the rule above and starting with 13, we generate the following sequence: | ||
-- 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 | ||
-- | ||
-- It can be seen that this sequence (starting at 13 and finishing at 1) contains | ||
-- 10 terms. Although it has not been proved yet (Collatz Problem), it is thought | ||
-- that all starting numbers finish at 1. | ||
-- | ||
-- Which starting number, under one million, produces the longest chain? | ||
-- | ||
-- NOTE: Once the chain starts the terms are allowed to go above one million. | ||
|
||
local function collatz_len(n, cache) | ||
if cache[n] then | ||
return cache[n] | ||
end | ||
|
||
local result | ||
if n == 1 then | ||
result = 0 | ||
elseif n % 2 == 0 then | ||
result = 1 + collatz_len(math.floor(n / 2), cache) | ||
else | ||
result = 2 + collatz_len(math.floor((3 * n + 1) / 2), cache) | ||
end | ||
|
||
cache[n] = result | ||
return result | ||
end | ||
|
||
return { | ||
solution = function() | ||
local max_seen = 0 | ||
local answer = 0 | ||
local cache = {} | ||
|
||
for i = 1,999999 do | ||
local result = collatz_len(i, cache) | ||
if result > max_seen then | ||
max_seen = result | ||
answer = i | ||
end | ||
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