summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2014-06-30 01:44:45 +0200
committerGuillermo Ramos2014-06-30 01:44:45 +0200
commitaef238a0600c5cb775bb371dbb2463fd0381bff9 (patch)
treee404730a40c3633726bf28093234f7a3030525cf
parent7db37912ec5762afd3ad67c68111e428f518bed0 (diff)
download7l-aef238a0600c5cb775bb371dbb2463fd0381bff9.tar.gz
[Lua] Día 1
-rw-r--r--lua/day1.lua46
1 files changed, 46 insertions, 0 deletions
diff --git a/lua/day1.lua b/lua/day1.lua
new file mode 100644
index 0000000..08c170d
--- /dev/null
+++ b/lua/day1.lua
@@ -0,0 +1,46 @@
+-- Easy
+function is_even(num)
+ return num % 2 == 0
+end
+
+function is_prime(num)
+ local res = true
+ for i = 2,num-1 do
+ if num % i == 0 then
+ res = false
+ end
+ end
+ return res
+end
+
+function print_first_n_primes(n)
+ local p = 1
+ while n ~= 0 do
+ repeat
+ p = p+1
+ until is_prime(p)
+ print(p)
+ n = n-1
+ end
+end
+
+-- Medium
+function for_loop(a, b, f)
+ while a < b do
+ f(a)
+ a = a+1
+ end
+end
+
+-- Hard
+function reduce(max, init, f)
+ local tmp = init
+ for i = 1,max do
+ tmp = f(tmp, i)
+ end
+ return tmp
+end
+
+function factorial(n)
+ return reduce(n, 1, function(x,y) return x*y end)
+end