From a0d8cd87b74114f101c4b7d49d0aa7d3a970f186 Mon Sep 17 00:00:00 2001 From: ergz Date: Fri, 24 Mar 2023 15:59:23 -0700 Subject: [PATCH] initial commit --- eight-queen.lua | 25 +++++++++++++++++++++++++ hello.lua | 16 ++++++++++++++++ lib1.lua | 8 ++++++++ 3 files changed, 49 insertions(+) create mode 100644 eight-queen.lua create mode 100644 hello.lua create mode 100644 lib1.lua diff --git a/eight-queen.lua b/eight-queen.lua new file mode 100644 index 0000000..c36eb05 --- /dev/null +++ b/eight-queen.lua @@ -0,0 +1,25 @@ +N = 8 -- size of the board + + +-- check whether position (n, c) is free from attacks +function is_place_ok (a, n, c) + for i = 1, n - 1 do -- leaving the inc/dec value empty defaults to +1 + if (a[i] == c) or + (a[i] - i == c - n) or + (a[i] + i == c + n) then + return false + end + end + return true +end + +function print_solution (a) + for i = 1, N, 1 do + for j = 1, N, 1 do + io.write(a[i] == j and "X" or "-", " ") + end + io.write("\n") + end + io.write("\n") +end + diff --git a/hello.lua b/hello.lua new file mode 100644 index 0000000..1280f39 --- /dev/null +++ b/hello.lua @@ -0,0 +1,16 @@ +print("hello world!") + +function fact (n) + if n < 0 then + print("factorial for a negative number is not defined!") + os.exit()m + elseif n == 0 then + return 1 + else + return n * fact(n - 1) + end +end + +print("enter a number") +a = io.read("*n") +print(fact(a)) diff --git a/lib1.lua b/lib1.lua new file mode 100644 index 0000000..611b9fc --- /dev/null +++ b/lib1.lua @@ -0,0 +1,8 @@ +function norm (x, y) + return math.sqrt(x^2 + y^2) +end + +function twice (x) + return 2.0 * x +end +