aboutsummaryrefslogtreecommitdiff
path: root/challenge-198/deadmarshal/lua
diff options
context:
space:
mode:
authordeadmarshal <adeadmarshal@gmail.com>2023-01-04 09:44:53 +0330
committerdeadmarshal <adeadmarshal@gmail.com>2023-01-04 09:44:53 +0330
commitdc6e6dc4252027b60ebd935abb1a53ce46145989 (patch)
treedba808345b6296ac45e7e53f7ee7046d503965d4 /challenge-198/deadmarshal/lua
parentee249218f373166edca2b95144a9b0b59e200e05 (diff)
downloadperlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.tar.gz
perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.tar.bz2
perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.zip
TWC198
Diffstat (limited to 'challenge-198/deadmarshal/lua')
-rw-r--r--challenge-198/deadmarshal/lua/ch-1.lua19
-rw-r--r--challenge-198/deadmarshal/lua/ch-2.lua28
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-198/deadmarshal/lua/ch-1.lua b/challenge-198/deadmarshal/lua/ch-1.lua
new file mode 100644
index 0000000000..84837b4562
--- /dev/null
+++ b/challenge-198/deadmarshal/lua/ch-1.lua
@@ -0,0 +1,19 @@
+#!/usr/bin/env lua
+
+local function max_gap(t)
+ local temp,count,max = 0,0,0
+ if #t < 2 then return 0 end
+ table.sort(t)
+ for i=1,#t,2 do
+ temp = math.abs(t[i] - t[i+1])
+ if temp > max then max = temp end
+ end
+ for i=1, #t-1 do
+ if math.abs(t[i] - t[i+1]) == max then count = count + 1 end
+ end
+ return count
+end
+
+print(max_gap({2,5,8,1}))
+print(max_gap({3}))
+
diff --git a/challenge-198/deadmarshal/lua/ch-2.lua b/challenge-198/deadmarshal/lua/ch-2.lua
new file mode 100644
index 0000000000..87f3653435
--- /dev/null
+++ b/challenge-198/deadmarshal/lua/ch-2.lua
@@ -0,0 +1,28 @@
+#!/usr/bin/env lua
+
+local function is_prime(n)
+ assert(type(n) == 'number', 'n must be a number!')
+ local i = 5
+ if n == 2 or n == 3 then return true end
+ if n <= 1 or n % 2 == 0 or n % 3 == 0 then return false end
+ while i * i <= n do
+ if n % i == 0 or n % (i+2) == 0 then return false end
+ i = i + 6
+ end
+ return true
+end
+
+local function prime_count(n)
+ assert(type(n) == 'number', 'n must be a number!')
+ local count = 0
+ for i=1,n-1 do
+ if is_prime(i) then count = count + 1 end
+ end
+ return count
+end
+
+print(prime_count(10))
+print(prime_count(15))
+print(prime_count(1))
+print(prime_count(25))
+