-
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
deccd48
commit b928d7b
Showing
14 changed files
with
233 additions
and
6 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,13 @@ | ||
math.lua | ||
======== | ||
|
||
View source code :source:`lua/src/lib/math.lua` | ||
|
||
.. lua:function:: factorial(n) | ||
:return: The factorial of n | ||
:rtype: number | ||
|
||
.. literalinclude:: ../../../../lua/src/lib/math.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
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 4 | ||
=============================== | ||
|
||
View source code :source:`lua/src/p0004.lua` | ||
|
||
Solution | ||
-------- | ||
|
||
.. lua:function:: solution() | ||
:return: The solution to problem 4 | ||
:rtype: number | ||
|
||
.. literalinclude:: ../../../lua/src/p0004.lua | ||
:language: Lua | ||
:linenos: | ||
|
||
.. tags:: palindrome |
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 34 | ||
================================ | ||
|
||
View source code :source:`lua/src/p0034.lua` | ||
|
||
Includes | ||
-------- | ||
|
||
- `math.lua <./lib/math.html>`__ | ||
|
||
Solution | ||
-------- | ||
|
||
.. lua:function:: solution() | ||
:return: The solution to problem 34 | ||
:rtype: number | ||
|
||
.. literalinclude:: ../../../lua/src/p0034.lua | ||
:language: Lua | ||
:linenos: | ||
|
||
.. tags:: factorial, digit-sum |
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 76 | ||
================================ | ||
|
||
View source code :source:`lua/src/p0076.lua` | ||
|
||
Solution | ||
-------- | ||
|
||
.. lua:function:: solution() | ||
:return: The solution to problem 76 | ||
:rtype: number | ||
|
||
.. literalinclude:: ../../../lua/src/p0076.lua | ||
:language: Lua | ||
:linenos: | ||
|
||
.. tags:: partition |
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,16 @@ | ||
-- This file is a direct port of Python's range functions | ||
|
||
local function factorial(n) | ||
local answer = 1 | ||
|
||
for i = 2,n,1 | ||
do | ||
answer = answer * i | ||
end | ||
|
||
return answer | ||
end | ||
|
||
return { | ||
factorial = factorial, | ||
} |
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,30 @@ | ||
-- Project Euler Problem 4 | ||
-- | ||
-- Problem: | ||
-- | ||
-- A palindromic number reads the same both ways. The largest palindrome made from | ||
-- the product of two 2-digit numbers is 9009 = 91 × 99. | ||
-- | ||
-- Find the largest palindrome made from the product of two 3-digit numbers. | ||
|
||
return { | ||
solution = function() | ||
local answer = 0 | ||
|
||
for v = 101,1001,1 | ||
do | ||
for u = 100,(v-1),1 | ||
do | ||
local p = u * v | ||
local ps = tostring(p) | ||
|
||
if ps == ps.reverse() and p > answer | ||
then | ||
answer = p | ||
end | ||
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
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,40 @@ | ||
-- Project Euler Problem 34 | ||
-- | ||
-- This ended up being a filtering problem. The problem with my solution is that I | ||
-- am not satisfied with my filter at all. I feel like there is a more efficient | ||
-- way to go about it. | ||
-- | ||
-- Problem: | ||
-- | ||
-- 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. | ||
-- | ||
-- Find the sum of all numbers which are equal to the sum of the factorial of | ||
-- their digits. | ||
-- | ||
-- Note: as 1! = 1 and 2! = 2 are not sums they are not included. | ||
|
||
return { | ||
solution = function() | ||
local answer = 0 | ||
|
||
for x = 10,99999,1 | ||
do | ||
local xs = tostring(x) | ||
local sum = 0 | ||
|
||
for i = 1,xs.len(),1 | ||
do | ||
sum = sum + factorial(tonumber(xs[i])) | ||
end | ||
|
||
if sum == x | ||
do | ||
answer = answer + 1 | ||
end | ||
end | ||
|
||
return answer | ||
end | ||
} | ||
|
||
local factorial = loadfile('src/lib/math.lua')().factorial |
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,62 @@ | ||
-- Project Euler Problem 34 | ||
-- | ||
-- This ended up being a filtering problem. The problem with my solution is that I | ||
-- am not satisfied with my filter at all. I feel like there is a more efficient | ||
-- way to go about it. | ||
-- | ||
-- Problem: | ||
-- | ||
-- 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. | ||
-- | ||
-- Find the sum of all numbers which are equal to the sum of the factorial of | ||
-- their digits. | ||
-- | ||
-- Note: as 1! = 1 and 2! = 2 are not sums they are not included. | ||
|
||
return { | ||
solution = function() | ||
local answer = 0 | ||
local idx = 0 | ||
local sum = 100 | ||
local counts = {} | ||
|
||
for i = 1, 101 do | ||
counts[i] = 0 | ||
end | ||
|
||
counts[2 + 1] = 0 | ||
|
||
while counts[100 + 1] == 0 | ||
do | ||
counts[2 + 1] = counts[2 + 1] + 2 | ||
|
||
if sum >= 100 | ||
then | ||
answer = answer + (100 + counts[2 + 1] - sum) / 2 | ||
idx = 2 | ||
|
||
repeat | ||
idx = idx + 1 | ||
counts[idx] = 0 -- please remember lua is 1-indexed | ||
idx = idx + 1 | ||
counts[idx] = counts[idx] + idx - 1 | ||
|
||
for i = (idx - 1),101,1 | ||
do | ||
sum = sum + counts[i] | ||
end | ||
until sum <= 100 | ||
end | ||
|
||
sum = 0 | ||
for i = 1,101,1 | ||
do | ||
sum = sum + counts[i] | ||
end | ||
end | ||
|
||
return answer | ||
end | ||
} | ||
|
||
local factorial = loadfile('src/lib/math.lua')().factorial |
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