Answer the question
In order to leave comments, you need to log in
Magic squares in Prolog?
Question for prologists, if there are any.
About two years ago I tried to programmatically search for “magic squares”, like this:
P O L O Z
O S O K A
L O T O K
O K O V A
Z A K A T
In Prolog, this should be, in theory, trivial. Write the facts of the form:
word('P', 'O', 'L', 'O', 'Z'). And make a query:
word(A1,A2,A3,A4,A5),word(A2,B1,B2,B3,B4),word(A3, B2,C1,C2,C3),word(A4,B3,C2 ,D1,D2),word(A5,B4,C3,D2 ,E1).
I wanted to generate such requests by Python for squares and rectangles of various sizes, find the words with Prolog and pass them to Python.
I tried to run a request for a 5 * 5 square on swi and broke off. I don’t remember how it ended there, either it hung, or gave a memory error, or I just waited a long time and did not wait. As a result, I just rewrote it in Python, it didn’t turn out so beautiful, of course, but it worked.
Either I don’t know how to cook, or this problem is too big for him and it’s worth taking another implementation of Prolog (what?), Or this method is not suitable for solving. In general, I will be glad to listen to smart people.
Here, if anything, is a list of five-letter Russian nouns - dl.dropboxusercontent.com/u/20473637/n.txt
Answer the question
In order to leave comments, you need to log in
If we take as a basis the code given at the link .
:- use_module(library(error)).
:- use_module(library(clpfd)).
:- use_module(library(lists)).
solve :-
load_file('n.txt', Words),
member([A1, A2, A3, A4, A5], Words),
member([A2, A6, A7, A8, A9], Words),
member([A3, A7, A10, A11, A12], Words),
member([A4, A8, A11, A13, A14], Words),
member([A5, A9, A12, A14, A15], Words),
writef("%s\n", ),
writef("%s\n", ),
writef("%s\n", ),
writef("%s\n", ),
writef("%s\n", ).
load_file(File, Words) :-
open(File, read, Stream, []),
call_cleanup(load_strings(Stream,Words), close(Stream)).
load_strings(Stream, Words) :-
read_line_to_codes(Stream, T0),
load_strings(T0, Stream, Words).
load_strings(end_of_file, _Stream, []) :- !.
load_strings([], _Stream, []) :- !.
load_strings(Line, Stream, [Line|Rest]) :-
read_line_to_codes(Stream, NextLine),
load_strings(NextLine, Stream, Rest).
abacus
badan
adept
marque
entre
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question