Skip to content

Commit

Permalink
Compress code
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 13, 2024
1 parent 6d03f70 commit 5cc6733
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 101 deletions.
2 changes: 1 addition & 1 deletion lua/src/lib/primes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ local function prime_factors(n)
return nil
end

while p and n % p ~= 0
while p and n % p
do
p = pgen.next()
end
Expand Down
9 changes: 3 additions & 6 deletions lua/src/p0001.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ return {
solution = function()
local answer = 0

for i = 3,999,3
do
for i = 3,999,3 do
answer = answer + i
end

for i = 5,999,5
do
for i = 5,999,5 do
answer = answer + i
end

for i = 15,999,15
do
for i = 15,999,15 do
answer = answer - i
end

Expand Down
6 changes: 2 additions & 4 deletions lua/src/p0002.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ return {
local a = 1
local b = 2

while b < 4000000
do
while b < 4000000 do
answer = answer + b

for _ = 1,3,1
do
for _ = 1,3 do
a, b = b, a + b
end
end
Expand Down
3 changes: 1 addition & 2 deletions lua/src/p0003.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ return {
local f = fgen.next()
local answer = 0

while f ~= nil
do
while f do
answer = f
f = fgen.next()
end
Expand Down
9 changes: 3 additions & 6 deletions lua/src/p0004.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ return {
solution = function()
local answer = 0

for v = 101,999,1
do
for u = 100,(v-1),1
do
for v = 101,999 do
for u = 100,(v-1) do
local p = u * v
local ps = tostring(p)

if ps == string.reverse(ps) and p > answer
then
if ps == string.reverse(ps) and p > answer then
answer = p
end
end
Expand Down
3 changes: 1 addition & 2 deletions lua/src/p0006.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ return {
local sum = 1
local sum_of_squares = 1

for i = 2,100,1
do
for i = 2,100 do
sum = sum + i
sum_of_squares = sum_of_squares + (i * i)
end
Expand Down
9 changes: 3 additions & 6 deletions lua/src/p0008.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ return {
.. "71636269561882670428252483600823257530420752963450")
local answer = 0

for i = 1,(#str-13),1
do
for i = 1,(#str-13) do
local product = 1
for j = i,(i+12),1
do
for j = i,(i+12) do
product = product * tonumber(str:sub(j, j))
end

if product > answer
then
if product > answer then
answer = product
end
end
Expand Down
12 changes: 4 additions & 8 deletions lua/src/p0009.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ return {
solution = function()
local c = 3

while true
do
while true do
local c_square = c * c

for b = 2,c,1
do
for b = 2,c do
local b_square = b * b

for a = 1,b,1
do
for a = 1,b do
local a_square = a * a

if a_square + b_square == c_square and a + b + c == 1000
then
if a_square + b_square == c_square and a + b + c == 1000 then
return a * b * c
end
end
Expand Down
24 changes: 8 additions & 16 deletions lua/src/p0011.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,33 @@ return {
local answer = 0
local tmp

for i = 1,20,1
do
for j = 1,17,1
do
for i = 1,20 do
for j = 1,17 do
-- horizontal section
tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3];
if tmp > answer
then
if tmp > answer then
answer = tmp
end

-- vertical section
tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i];
if tmp > answer
then
if tmp > answer then
answer = tmp
end
end
end

for i = 1,17,1
do
for j = 1,17,1
do
for i = 1,17 do
for j = 1,17 do
-- right diagonal section
tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3];
if tmp > answer
then
if tmp > answer then
answer = tmp
end

-- left diagonal section
tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j];
if tmp > answer
then
if tmp > answer then
answer = tmp
end
end
Expand Down
15 changes: 5 additions & 10 deletions lua/src/p0013.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,20 @@ local ten10 = 10000000000
return {
solution = function()
local arr = { 0, 0, 0 }
for i = 1,100,1
do
for j = 1,3,1
do
for i = 1,100 do
for j = 1,3 do
arr[j] = arr[j] + numbers[i][j]
end

for j = 2,1,-1
do
if arr[j] > ten18
then
for j = 2,1,-1 do
if arr[j] > ten18 then
arr[j - 1] = math.floor(arr[j - 1] + arr[j] / ten18)
arr[j] = arr[j] % ten18
end
end
end

while arr[1] > ten10
do
while arr[1] > ten10 do
arr[1] = math.floor(arr[1] / 10)
end

Expand Down
21 changes: 7 additions & 14 deletions lua/src/p0017.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@
-- British usage.

local function to_string_len(n)
if n >= 1000
then
if n >= 1000 then
local thousands = to_string_len(math.floor(n / 1000 % 100)) + 8

if n % 1000 ~= 0
then
if n % 1000 then
thousands = thousands + to_string_len(n % 1000)
end

return thousands
end

if n >= 100
then
if n >= 100 then
local hundreds = to_string_len(math.floor(n / 100 % 10)) + 7

if n % 100 ~= 0
then
if n % 100 then
hundreds = hundreds + 3 + to_string_len(n % 100)
end

return hundreds
end

if n >= 20
then
if n >= 20 then
local tens_t = {
[2] = 6,
[3] = 6,
Expand All @@ -54,8 +49,7 @@ local function to_string_len(n)
}
local tens = tens_t[math.floor(n / 10)]

if n % 10 ~= 0
then
if n % 10 then
tens = tens + to_string_len(n % 10)
end

Expand Down Expand Up @@ -91,8 +85,7 @@ return {
solution = function()
local answer = 0

for x = 1,1000,1
do
for x = 1,1000 do
answer = answer + to_string_len(x)
end

Expand Down
3 changes: 1 addition & 2 deletions lua/src/p0028.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ return {
solution = function()
local answer = 1

for i = 1,(1000 / 2),1
do
for i = 1,(1000 / 2) do
local start = (2 * i - 1)^2 + 1
answer = answer + range_entry(start, 1, (1 * 2 * i - 1))
answer = answer + range_entry(start, 1, (2 * 2 * i - 1))
Expand Down
9 changes: 3 additions & 6 deletions lua/src/p0034.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ return {
solution = function()
local answer = 0

for x = 10,99999,1
do
for x = 10,99999 do
local xs = tostring(x)
local sum = 0
for i = 1,#xs,1
do
for i = 1,#xs do
sum = sum + factorial(tonumber(xs:sub(i, i)))
end

if sum == x
then
if sum == x then
answer = answer + x
end
end
Expand Down
15 changes: 5 additions & 10 deletions lua/src/p0076.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ return {
local sum = 100
local counts = {}

for i = 2,100,1
do
for i = 2,100 do
counts[i] = 0
end

counts[2] = 100

while counts[100] == 0
do
while counts[100] == 0 do
counts[2] = counts[2] + 2

if sum >= 100
then
if sum >= 100 then
answer = answer + math.floor((100 + counts[2] - sum) / 2)
idx = 2

Expand All @@ -42,16 +39,14 @@ return {
counts[idx] = counts[idx] + idx

sum = counts[idx]
for i = (idx+1),100,1
do
for i = (idx+1),100 do
sum = sum + counts[i]
end
until sum <= 100
end

sum = counts[2]
for i = 3,100,1
do
for i = 3,100 do
sum = sum + counts[i]
end
end
Expand Down
12 changes: 4 additions & 8 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ function loadlib(...)
local length = select("#", ...)

local lib, err = loadfile("src/lib/" .. select(1, ...) .. ".lua")
if not lib
then
if not lib then
error("Failed to load lib " .. libname .. ": " .. err)
end
lib = lib()

if length == 1
then
if length == 1 then
return lib
end

local ret = {}
for i = 2,length,1
do
for i = 2,length do
local fname = select(i, ...)
ret[fname] = lib[fname]
end
Expand Down Expand Up @@ -46,8 +43,7 @@ end
local function check_problem(file_name, expected_answer, is_slow, problem_name)
print("Starting: " .. file_name)
local problem_func = load_problem("src/" .. file_name)
if is_slow and has_luacov
then
if is_slow and has_luacov then
return
end
local start_time = os.clock()
Expand Down

0 comments on commit 5cc6733

Please sign in to comment.