safe(Boardsize, Configuration) :- buildupto(Boardsize, RevConfig, 0, []), reverse(RevConfig, Configuration). buildupto(Boardsize, Configuration, Boardsize, Configuration). buildupto(Boardsize, Configuration, Currentcol, PartialConfig) :- Newcol is Currentcol+1, ibt(1, Boardsize, Newrow), noclash([Newrow, Newcol], PartialConfig), buildupto(Boardsize, Configuration, Newcol, [[Newrow, Newcol] | PartialConfig]). noclash([I,J], []). noclash([I,J], [[K,L] | X]) :- I\==K, J\==L, D1 is I-J, D2 is K-L, D1\==D2, S1 is I+J, S2 is K+L, S1\==S2, noclash([I,J], X). ibt(L, H, I) :- number(I), !, L =< I, I =< H. ibt(L, H, I) :- I is L. ibt(L, H, I) :- L1 is L+1, L1 =< H, ibt(L1, H, I).