Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 12, 2024
1 parent b2f3199 commit 6a7c6c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 15 additions & 9 deletions lua/src/lib/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ local function factorial(n)
return answer
end

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

-- slow path for big numbers// slow path for larger numbers
local function factor_gen(n, r)
local factors = {}
local answer
local tmp

-- collect factors of final number
for i = 2,n,1
Expand Down Expand Up @@ -51,6 +43,19 @@ local function n_choose_r(n, r)
end
end

return factors
end

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

-- slow path for big numbers// slow path for larger numbers
local factors = factor_gen(n, r)
local answer
local tmp
local i = 2
local j = 2
answer = 1;
Expand Down Expand Up @@ -95,4 +100,5 @@ end

return {
factorial = factorial,
n_choose_r = n_choose_r,
}
4 changes: 2 additions & 2 deletions lua/src/p0015.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
-- Turns out this is easy, if you think sideways a bit
--
-- You can only go down or right. If we say right=1, then you can only have 20 1s, since otherwise you go off the grid.
-- You also can't have fewer than 20 1s, since then you go off the grid the other way. This means you can look at it as a
-- bit string, and the number of 40-bit strings with 20 1s is 40c20.
-- You also can't have fewer than 20 1s, since then you go off the grid the other way. This means you can look at it as
-- a bit string, and the number of 40-bit strings with 20 1s is 40c20.
--
-- Problem:
--
Expand Down

0 comments on commit 6a7c6c1

Please sign in to comment.