From aef238a0600c5cb775bb371dbb2463fd0381bff9 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Mon, 30 Jun 2014 01:44:45 +0200 Subject: [Lua] Día 1 --- lua/day1.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lua/day1.lua (limited to 'lua/day1.lua') 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 -- cgit v1.2.3