aboutsummaryrefslogtreecommitdiff
path: root/comparison/Python
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2022-08-28 20:40:57 +0900
committerShinya Sato <shinnya.sato@gmail.com>2022-08-28 20:40:57 +0900
commit02a84743ddf5c8c27a36c4d5a20eaf3cd2a9aff4 (patch)
tree8bb368481caaa87f281358bb746edcc881b7e54c /comparison/Python
parent3547d92c3601f8c6c295636cd2394b1d85c31bc1 (diff)
downloadinpla-02a84743ddf5c8c27a36c4d5a20eaf3cd2a9aff4.tar.gz
inpla-02a84743ddf5c8c27a36c4d5a20eaf3cd2a9aff4.zip
Update for n-queens
Diffstat (limited to '')
-rw-r--r--comparison/Python/Makefile2
-rw-r--r--comparison/Python/fib-38.py8
-rw-r--r--comparison/Python/nqueen-12.py27
3 files changed, 36 insertions, 1 deletions
diff --git a/comparison/Python/Makefile b/comparison/Python/Makefile
index c77d677..5dd95fd 100644
--- a/comparison/Python/Makefile
+++ b/comparison/Python/Makefile
@@ -1,5 +1,5 @@
COMMAND = python3
-TARGET = dummy.py ack3-11.py fib38.py bsort-20000.py isort-20000.py qsort-260000.py msort-260000.py
+TARGET = dummy.py nqueen-12.py ack3-11.py fib-38.py bsort-20000.py isort-20000.py qsort-260000.py msort-260000.py
BENCHMARK = bash ../tools/sptest_scripts.sh
LOG = log.txt
diff --git a/comparison/Python/fib-38.py b/comparison/Python/fib-38.py
new file mode 100644
index 0000000..efa5380
--- /dev/null
+++ b/comparison/Python/fib-38.py
@@ -0,0 +1,8 @@
+def fib(n):
+ if n == 0:
+ return 0
+ elif n == 1:
+ return 1
+ else:
+ return fib(n-1) + fib(n-2)
+print(fib(38))
diff --git a/comparison/Python/nqueen-12.py b/comparison/Python/nqueen-12.py
new file mode 100644
index 0000000..494131f
--- /dev/null
+++ b/comparison/Python/nqueen-12.py
@@ -0,0 +1,27 @@
+def threat(k, m, alist):
+ if alist == []:
+ return False
+ else:
+ x = alist[0]
+ if (k == x-m) or (k == m-x):
+ return True
+ else:
+ return threat(k+1, m, alist[1:])
+
+def queen1(m, b, n):
+ if m == 0:
+ return []
+ else:
+ if (m in b) or threat(1, m, b):
+ return queen1(m-1, b, n)
+ elif len(b) == n-1:
+ return [[m]+b] + (queen1(m-1, b, n))
+ else:
+ return queen1(n, [m]+b, n) + (queen1(m-1, b, n))
+
+
+def queen(n):
+ return queen1(n,[],n)
+
+n=12
+print(len(queen(12)))