<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">

% Sudoku
% Martti Meri


% 4X4 table
sudim(2).

% full Sudoku table
%sudim(3).

suset(S) :-
    sudim(SD),
    L is SD * SD,
    numlist(1,L,S).

bad_cell(S,I,J,X) :-
    member([I2,J2,X2],S),
    sudim(SD),
    CI is ((I-1) // SD),
    CI2 is ((I2-1) // SD),
    CJ is ((J-1) // SD), 
    CJ2 is ((J2-1) // SD),
    CI = CI2,
    CJ = CJ2,
    X = X2.

initJ(S,_,L,S) :-
    sudim(SD),
    L1 is ((SD * SD) + 1),
    L = L1.

initJ(S,I,J,R) :-
    JP is J + 1,
    suset(SS),
    member(X,SS),
    \+member([I,J,_],S),
    \+member([I,_,X],S),
    \+member([_,J,X],S),
    \+bad_cell(S,I,J,X),
    initJ([[I,J,X]|S],I,JP,R).

initI(S,L,S) :-
    sudim(SD),
    L1 is ((SD * SD) + 1),
    L = L1.

initI(S,I,R) :-
    initJ(S,I,1,R1),
    IP is I+1,
    initI(R1,IP,R).

printI([]) :- 
    nl.

printI([[I,J,X] | T]) :-
    sudim(SD),
    JD is SD * SD,
    J = JD,
    !,
    printI(T),
    write([I,J,X]),
    nl.

printI([[I,J,X] | T]) :-
    printI(T),
    write([I,J,X]),
    tab(1).
    
    
main1(S) :-
    initI([],1,S).

lex(S) :-
    main1(S),
    printI(S).

</pre></body></html>