aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/stuart-little/lua/ch-1.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-079/stuart-little/lua/ch-1.lua')
-rwxr-xr-xchallenge-079/stuart-little/lua/ch-1.lua13
1 files changed, 13 insertions, 0 deletions
diff --git a/challenge-079/stuart-little/lua/ch-1.lua b/challenge-079/stuart-little/lua/ch-1.lua
new file mode 100755
index 0000000000..ed17d5faac
--- /dev/null
+++ b/challenge-079/stuart-little/lua/ch-1.lua
@@ -0,0 +1,13 @@
+#!/usr/bin/env lua
+
+-- run <script> <number>
+-- https://oeis.org/A000788
+
+function bitsUpTo(n)
+ if n <= 1 then return n end
+ local topBitPos = math.floor(math.log(n,2))
+ local rest = n ~ 2^topBitPos
+ return 1+topBitPos*2^(topBitPos-1) + rest + bitsUpTo(rest)
+end
+
+print(("%d"):format(bitsUpTo(tonumber(arg[1]))))