Skip to content

Commit

Permalink
Compress code (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 13, 2024
1 parent 5cc6733 commit dc7d9c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 57 deletions.
45 changes: 15 additions & 30 deletions lua/src/lib/math.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local function factorial(n)
local answer = 1

for i = 2,n,1
do
for i = 2,n do
answer = answer * i
end

Expand All @@ -13,28 +12,22 @@ local function factor_gen(n, r)
local factors = {}

-- collect factors of final number
for i = 2,n,1
do
for i = 2,n do
factors[i] = 1
end

-- negative factor values indicate need to divide
for i = 2,r,1
do
for i = 2,r do
factors[i] = factors[i] - 1
end
for i = 2,(n - r),1
do
for i = 2,(n - r) do
factors[i] = factors[i] - 1
end

-- this loop reduces to prime factors only
for i = n,2,-1
do
for j = 2,(i - 1),1
do
if i % j == 0
then
for i = n,2,-1 do
for j = 2,(i - 1) do
if i % j == 0 then
factors[j] = factors[j] + factors[i]
factors[i / j] = factors[i / j] + factors[i]
factors[i] = 0
Expand All @@ -47,8 +40,7 @@ local function factor_gen(n, r)
end

local function n_choose_r(n, r)
if n < 20 -- fast path if number is small
then
if n < 20 then -- fast path if number is small
return factorial(n) / factorial(r) / factorial(n - r)
end

Expand All @@ -59,36 +51,29 @@ local function n_choose_r(n, r)
local i = 2
local j = 2
answer = 1;
while i <= n
do
while factors[i] > 0
do
while i <= n do
while factors[i] > 0 do
tmp = answer
answer = answer * i
while answer < tmp and j <= n
do
while factors[j] < 0
do
while answer < tmp and j <= n do
while factors[j] < 0 do
tmp = math.floor(tmp / j)
factors[j] = factors[j] + 1
end
j = j + 1;
answer = tmp * i
end

if answer < tmp
then
if answer < tmp then
return nil, "math error: overflow"
end
factors[i] = factors[i] - 1
end
i = i + 1
end

while j <= n
do
while factors[j] < 0
do
while j <= n do
while factors[j] < 0 do
answer = answer / j
factors[j] = factors[j] + 1
end
Expand Down
24 changes: 8 additions & 16 deletions lua/src/lib/primes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ local function primes(stop)
local function next_prime(cand)
local steps = sieve[cand]

if steps
then
for _, step in ipairs(steps)
do
if steps then
for _, step in ipairs(steps) do
local value = cand + step
if sieve[value]
then
if sieve[value] then
table.insert(sieve[value], step)
else
sieve[value] = { step }
Expand All @@ -27,8 +24,7 @@ local function primes(stop)
end

local function next()
if stop and prime >= stop
then
if stop and prime >= stop then
sieve = {}
error("Tried to exceed given limit")
end
Expand All @@ -48,23 +44,19 @@ local function prime_factors(n)
local p = pgen.next()

local function next()
if n == 1
then
if n == 1 then
return nil
end

while p and n % p
do
while p and n % p ~= 0 do
p = pgen.next()
end

if p == nil
then
if p == nil then
return nil
end

if n < 0
then
if n < 0 then
n = -n
return -1
end
Expand Down
12 changes: 4 additions & 8 deletions lua/src/lib/range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ end

local function range_entry4(start, stop, step, idx)
local length = 0
if step > 0 and start < stop
then
if step > 0 and start < stop then
length = math.floor(1 + (stop - 1 - start) / step)
elseif step < 0 and start > stop
then
elseif step < 0 and start > stop then
length = math.floor(1 + (start - 1 - stop) / (-step))
end
if idx < 0
then
if idx < 0 then
idx = length + idx
end
if idx < 0 or idx >= length
then
if idx < 0 or idx >= length then
return nil, "length is 0, cannot fetch"
end
return range_entry3(start, step, idx)
Expand Down
6 changes: 3 additions & 3 deletions lua/src/p0017.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local function to_string_len(n)
if n >= 1000 then
local thousands = to_string_len(math.floor(n / 1000 % 100)) + 8

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

Expand All @@ -29,7 +29,7 @@ local function to_string_len(n)
if n >= 100 then
local hundreds = to_string_len(math.floor(n / 100 % 10)) + 7

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

Expand All @@ -49,7 +49,7 @@ local function to_string_len(n)
}
local tens = tens_t[math.floor(n / 10)]

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

Expand Down

0 comments on commit dc7d9c8

Please sign in to comment.