aboutsummaryrefslogtreecommitdiff
path: root/challenge-102/paulo-custodio/lua
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-03-15 18:18:09 +0800
committer冯昶 <seaker@qq.com>2021-03-15 18:18:09 +0800
commit5ed25077fde85262036c9db3e893d70ae0907b5c (patch)
tree8932d25b3fa6076e2d91ab2a331d4d8bfff20544 /challenge-102/paulo-custodio/lua
parent8b6be37fe4dac8b4c6489a95e55514b76b298d15 (diff)
parent65d54d52500028ec5359a7d39619803ade281543 (diff)
downloadperlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.tar.gz
perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.tar.bz2
perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-102/paulo-custodio/lua')
-rw-r--r--challenge-102/paulo-custodio/lua/ch-1.lua43
-rw-r--r--challenge-102/paulo-custodio/lua/ch-2.lua46
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-102/paulo-custodio/lua/ch-1.lua b/challenge-102/paulo-custodio/lua/ch-1.lua
new file mode 100644
index 0000000000..a24f601c57
--- /dev/null
+++ b/challenge-102/paulo-custodio/lua/ch-1.lua
@@ -0,0 +1,43 @@
+#!/usr/bin/env lua
+
+--[[
+Challenge 102
+
+TASK #1 › Rare Numbers
+Submitted by: Mohammad S Anwar
+
+You are given a positive integer $N.
+
+Write a script to generate all Rare numbers of size $N if exists. Please
+checkout the page for more information about it.
+Examples
+
+(a) 2 digits: 65
+(b) 6 digits: 621770
+(c) 9 digits: 281089082
+--]]
+
+function perfect_square(n)
+ local sq = math.sqrt(n)
+ if math.floor(sq) == sq then
+ return true
+ else
+ return false
+ end
+end
+
+function print_rare(n)
+ for r=10^(n-1),(10^n)-1 do
+ r = math.floor(r) -- convert to int
+ r1 = tonumber(string.reverse(tostring(r)))
+ if perfect_square(r+r1) then
+ if r >= r1 then
+ if perfect_square(r-r1) then
+ io.write(r, "\n")
+ end
+ end
+ end
+ end
+end
+
+print_rare(tonumber(arg[1]))
diff --git a/challenge-102/paulo-custodio/lua/ch-2.lua b/challenge-102/paulo-custodio/lua/ch-2.lua
new file mode 100644
index 0000000000..72d378f496
--- /dev/null
+++ b/challenge-102/paulo-custodio/lua/ch-2.lua
@@ -0,0 +1,46 @@
+#!/usr/bin/env lua
+
+--[[
+Challenge 102
+
+TASK #2 > Hash-counting String
+Submitted by: Stuart Little
+
+You are given a positive integer $N.
+
+Write a script to produce Hash-counting string of that length.
+
+The definition of a hash-counting string is as follows:
+- the string consists only of digits 0-9 and hashes, '#'
+- there are no two consecutive hashes: '##' does not appear in your string
+- the last character is a hash
+- the number immediately preceding each hash (if it exists) is the position
+of that hash in the string, with the position being counted up from 1
+
+It can be shown that for every positive integer N there is exactly one such
+length-N string.
+Examples:
+
+(a) "#" is the counting string of length 1
+(b) "2#" is the counting string of length 2
+(c) "#3#" is the string of length 3
+(d) "#3#5#7#10#" is the string of length 10
+(e) "2#4#6#8#11#14#" is the string of length 14
+--]]
+
+function hash_counting(n)
+ local out, i = '', n
+ while i > 0 do
+ p = i
+ out = '#'..out
+ i = i - 1
+ while i > 0 and p ~= 0 do
+ out = tostring(p % 10)..out
+ i = i - 1
+ p = math.floor(p / 10)
+ end
+ end
+ return out
+end
+
+print(hash_counting(tonumber(arg[1])))