From 02a84743ddf5c8c27a36c4d5a20eaf3cd2a9aff4 Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Sun, 28 Aug 2022 20:40:57 +0900 Subject: Update for n-queens --- comparison/Python/Makefile | 2 +- comparison/Python/fib-38.py | 8 ++++++++ comparison/Python/nqueen-12.py | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 comparison/Python/fib-38.py create mode 100644 comparison/Python/nqueen-12.py (limited to 'comparison/Python') 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))) -- cgit v1.2.3