-
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
13a15e9
commit 9c8a1e1
Showing
7 changed files
with
55 additions
and
5 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,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 |
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,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 | ||
} |
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