Search code examples
luadynamic-programmingcoin-change

Dynamic Programming Change Maker


I'm attempting to convert the following algorithm, and I've gotten it mostly working however there's an example from the book I'm using which says I input the denominations of 1,3,4 and a n value of 6 and receive the output of 2.

--[[
ALGORITHM ChangeMaking(D[1..m], n)
//Applies dynamic programming to find the minimum number of coins
//of denominations d1< d2 < . . . < dm where d1 = 1 that add up to a
//given amount n
//Input: Positive integer n and array D[1..m] of increasing positive
// integers indicating the coin denominations where D[1]= 1
//Output: The minimum number of coins that add up to n
F[0]←0
for i ←1 to n do
temp←∞; j ←1
while j ≤ m and i ≥ D[j ] do
temp ←min(F [i − D[j ]], temp)
j ←j + 1
F[i]←temp + 1
return F[n]
]]

The Following is my attempts to convert the code, and get it working. I've run into a few problems, when trying to set temp = math.if I get an error saying number expected, but received nil, So i swapped it to math.huge and it works but it doesn't return an output of 2 but rather nil.

function ChangeMaking(D,n)
--[[
//Applies dynamic programming to find the minimum number of coins
//of denominations d1< d2 < . . . < dm where d1 = 1 that add up to a
//given amount n
//Input: Positive integer n and array D[1..m] of increasing positive
// integers indicating the coin denominations where D[1]= 1
//Output: The minimum number of coins that add up to n
]]
F = {}
m = tablelength(D)
F[0] = 0
  for i =1,n do
    temp = math.inf
    j = 1
    while j <= m and i >= D[j] do
      temp = math.min(F[ i - D[j] ], temp)
      j = j + 1
    end
    F[i] = temp + 1
  return F[n]
  end
end
function main()
  print("Hello Welcome the to Change Maker - LUA Edition")
  print("Enter a series of change denominations, separated by spaces")
  input = io.read()
  deno = {}
   for num in input:gmatch("%d+") do table.insert(deno,tonumber(num)) end
  local i = 1
  while i ~= 0 do
    print("Please Enter Total for Change Maker")
    input2 = io.read("*n")
    if input2 == 0 then i=0 end
    print(ChangeMaking(deno,input2))
  end
end
function tablelength(T)
--[[
//Function for grabbing the total length of a table.
]]
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
main()
--[[
    OUTPUT
    Hello Welcome the to Change Maker - LUA Edition
    Enter a series of change denominations, separated by spaces
    1 3 4
    Please Enter Total for Change Maker
    6
    nil
]]

Solution

  • The return statement is in the wrong place. It needs to be outside of the for loop. In your version, the for loop iterates once and then the function returns F[1], which is nil.

    function ChangeMaking(D, n)
        F = {}
        m = tablelength(D)
        F[0] = 0
        for i = 1, n do
            temp = math.huge
            j = 1
            while j <= m and i >= D[j] do
                temp = math.min(F[ i - D[j] ], temp)
                j = j + 1
            end
            F[i] = temp + 1
        end
        return F[n]
    end