Skip to content

Commit

Permalink
Solve p16 in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 10, 2024
1 parent 13a15e9 commit 9c8a1e1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Olivia's Project Euler Solutions
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
+------------+----------------------------+--------+-------------------+
| Lua | PUC-Rio Lua 5+ [#]_ | 20 | |Luai| |br| |
| Lua | PUC-Rio Lua 5+ [#]_ | 21 | |Luai| |br| |
| | | | |Lu-Cov| |br| |
| | | | |LuaCheck| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`015`|:c-d:`0015`|:cp-d:`0015`|:cs-d:`0015`|:fr-d:`0015`|:ja-d:`0015`|:js-d:`0015`|:lu-d:`0015`|:py-d:`0015`|:rs-d:`0015`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`016`|:c-d:`0016`|:cp-d:`0016`|:cs-d:`0016`|:fr-d:`0016`|:ja-d:`0016`|:js-d:`0016`| |:py-d:`0016`|:rs-d:`0016`|
|:prob:`016`|:c-d:`0016`|:cp-d:`0016`|:cs-d:`0016`|:fr-d:`0016`|:ja-d:`0016`|:js-d:`0016`|:lu-d:`0016`|:py-d:`0016`|:rs-d:`0016`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`017`|:c-d:`0017`|:cp-d:`0017`|:cs-d:`0017`|:fr-d:`0017`|:ja-d:`0017`|:js-d:`0017`|:lu-d:`0017`|:py-d:`0017`|:rs-d:`0017`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
15 changes: 15 additions & 0 deletions docs/src/lua/p0016.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Lua Implementation of Problem 16
================================

View source code :source:`lua/src/p0016.lua`

.. lua:function:: solution()
:return: The solution to problem 16
:rtype: number

.. literalinclude:: ../../../lua/src/p0016.lua
:language: Lua
:linenos:

.. tags:: large-numbers, digit-sum, power
2 changes: 1 addition & 1 deletion fortran/src/p0016.f90
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pure integer(i18t) function p0016() result(answer)
end do
do j = 1, size(numbers) - 1
if (numbers(j) > ten17) then
numbers(j + 1) = numbers(j + 1) + numbers(j) / ten17;
numbers(j + 1) = numbers(j + 1) + numbers(j) / ten17
numbers(j) = mod(numbers(j), ten17)
end if
end do
Expand Down
1 change: 1 addition & 0 deletions lua/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Problems Solved
- ☒ `13 <./src/p0013.lua>`__
- ☒ `14 <./src/p0014.lua>`__
- ☒ `15 <./src/p0015.lua>`__
- ☒ `16 <./src/p0016.lua>`__
- ☒ `17 <./src/p0017.lua>`__
- ☒ `22 <./src/p0022.lua>`__
- ☒ `28 <./src/p0028.lua>`__
Expand Down
35 changes: 35 additions & 0 deletions lua/src/p0016.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- Project Euler Problem 15
--
-- Problem:
--
-- 2**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
--
-- What is the sum of the digits of the number 2**1000?

return {
solution = function()
local numbers = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
local ten15 = 1000000000000000
local power = 1
local answer = 0

for i = 1,1000 do
for j = 1,#numbers do
numbers[j] = numbers[j] * 2
end
for j = 1,(#numbers - 1) do
if (numbers[j] > ten15) then
numbers[j + 1] = numbers[j + 1] + math.floor(numbers[j] / ten15)
numbers[j] = numbers[j] % ten15
end
end
end
for i = 1, 18 do
for j = 1,#numbers do
answer = answer + math.floor(numbers[j] / power) % 10
end
power = power * 10
end
return answer
end
}
3 changes: 1 addition & 2 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ local problems = {}
for i = 1, 11 do
problems[string.format("p%04d.lua", i)] = {get_answer(i), false}
end
for i = 13, 15 do
for i = 13, 17 do
problems[string.format("p%04d.lua", i)] = {get_answer(i), false}
end

local more_problems = {
["p0017.lua"] = {get_answer(17), false},
["p0022.lua"] = {get_answer(22), false},
["p0028.lua"] = {get_answer(28), false},
["p0034.lua"] = {get_answer(34), false},
Expand Down

0 comments on commit 9c8a1e1

Please sign in to comment.