aboutsummaryrefslogtreecommitdiff
path: root/challenge-118/abigail/lua/ch-1.lua
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-06-27 20:22:30 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-06-27 20:22:30 +0100
commit287d70fb03bbb2ba51e636cf051775400e94bcb4 (patch)
tree85a39b2a331bbb5b49d17f63ae76796f3999e7ce /challenge-118/abigail/lua/ch-1.lua
parent9beb73e6913f7e462c4aca8719048a7be98e4ad2 (diff)
parent1b3142a75a8db8a886596770514e2d9fa1cd9187 (diff)
downloadperlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.gz
perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.bz2
perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-118/abigail/lua/ch-1.lua')
-rw-r--r--challenge-118/abigail/lua/ch-1.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-118/abigail/lua/ch-1.lua b/challenge-118/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..a129db19e4
--- /dev/null
+++ b/challenge-118/abigail/lua/ch-1.lua
@@ -0,0 +1,32 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+--
+-- Find the binary representation of a number.
+-- Note that the function return a string in reverse order;
+-- this will do for our purpose as we want a palindrome anyway.
+--
+function dec2bin (dec)
+ local bin = {}
+ while dec > 0 do
+ bin [#bin + 1] = dec % 2
+ dec = math . floor (dec / 2)
+ end
+ return table . concat (bin)
+end
+
+for line in io . lines () do
+ bin = dec2bin (tonumber (line))
+ if bin == string . reverse (bin) then
+ print (1)
+ else
+ print (0)
+ end
+end