aboutsummaryrefslogtreecommitdiff
path: root/challenge-085
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-085')
-rwxr-xr-xchallenge-085/tyler-wardhaugh/lua/ch-2.lua22
-rwxr-xr-xchallenge-085/tyler-wardhaugh/lua/test.lua9
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-085/tyler-wardhaugh/lua/ch-2.lua b/challenge-085/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..6af287ab8d
--- /dev/null
+++ b/challenge-085/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,22 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.has_power_expr(n)
+ local endpoint = function(x) return 1 + math.floor(math.log(n, x)) end
+ for a = 2,endpoint(2) do
+ for b = 2,endpoint(a) do
+ if n == a ^ b then
+ return 1
+ end
+ end
+ end
+
+ return 0
+end
+
+function t2.run(args)
+ print(t2.has_power_expr(tonumber(args[1])))
+end
+
+return t2
diff --git a/challenge-085/tyler-wardhaugh/lua/test.lua b/challenge-085/tyler-wardhaugh/lua/test.lua
index 4577757e03..fdac1e33d8 100755
--- a/challenge-085/tyler-wardhaugh/lua/test.lua
+++ b/challenge-085/tyler-wardhaugh/lua/test.lua
@@ -18,3 +18,12 @@ describe("Task 1, Triplet Sum", function()
assert.are.same(1, t1.find_triplet_sum(big))
end)
end)
+
+describe("Task 2, Power of Two Integers", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(1, t2.has_power_expr(8))
+ assert.are.same(0, t2.has_power_expr(15))
+ assert.are.same(1, t2.has_power_expr(125))
+ end)
+end)