aboutsummaryrefslogtreecommitdiff
path: root/challenge-090/tyler-wardhaugh/lua/ch-2.lua
blob: 85c69b7be838f5904503cab6564c28c5ff7a1488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env lua

local t2 = {}

function t2.ethiopian_multiply(a, b)
  local product = 0

  while a > 0 do
    if a % 2 ~= 0 then
      product = product + b
    end

    a = a // 2
    b = b * 2
  end

  return product
end

function t2.run(args)
  local A = tonumber(args[1]) or 12
  local B = tonumber(args[2]) or 14
  local product = t2.ethiopian_multiply(A, B)
  print(product)
end

return t2