# Copyright (C) 2008 Toby Thain, toby@telegraphics.com.au # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # GNU Makefile .PHONY : all db solve clean DB = vierneun MYSQLOPTS = -u root all : vn db vn : vn.c db : mysqladmin $(MYSQLOPTS) create $(DB) mysql $(MYSQLOPTS) $(DB) -B -e \ 'CREATE TABLE squares ( root TINYINT PRIMARY KEY, \ a TINYINT, b TINYINT, c TINYINT, d TINYINT, \ KEY (b) )' ( for (( I=32; I<=99; ++I )) ; do \ echo "SET @sq:=$$I*$$I; \ INSERT INTO squares (root, a, b, c, d) \ VALUES ($$I, @sq DIV 1000, MOD(@sq DIV 100,10), MOD(@sq DIV 10,10), MOD(@sq,10));" ; \ done ) | mysql -B $(MYSQLOPTS) $(DB) solve : mysql $(MYSQLOPTS) $(DB) -t -B -e \ 'DROP TABLE IF EXISTS t; \ CREATE TABLE t \ SELECT v.root AS v, n.root AS n \ FROM squares AS v JOIN squares AS n ON n.b=v.c \ WHERE n.a=n.d \ AND v.a NOT IN (v.b,v.c,v.d,n.a,n.c) \ AND v.b NOT IN ( v.c,v.d,n.a,n.c) \ AND v.c NOT IN ( v.d,n.a,n.c) \ AND v.d NOT IN ( n.a,n.c) \ AND n.a<>n.c; \ SELECT a,b,c,d FROM t AS t1 JOIN squares AS s ON root IN (v,n) \ WHERE NOT EXISTS( SELECT * FROM t WHERE (v<>t1.v AND n=t1.n) \ OR (n<>t1.n AND v=t1.v) )' clean : rm -f vn mysqladmin $(MYSQLOPTS) drop $(DB)