Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:16:47+00:00 2026-06-16T00:16:47+00:00

I am trying to write a program in Prolog to find a Latin Square

  • 0

I am trying to write a program in Prolog to find a Latin Square of size N.

I have this right now:

delete(X, [X|T], T).
delete(X, [H|T], [H|S]) :-
   delete(X, T, S).

permutation([], []).
permutation([H|T], R) :-
   permutation(T, X),
   delete(H, R, X).

latinSqaure([_]).
latinSquare([A,B|T], N) :-
   permutation(A,B),
   isSafe(A,B),
   latinSquare([B|T]).

isSafe([], []).
isSafe([H1|T1], [H2|T2]) :- 
   H1 =\= H2, 
   isSafe(T1, T2).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:16:47+00:00Added an answer on June 16, 2026 at 12:16 am

    using SWI-Prolog library:

    :- module(latin_square, [latin_square/2]).
    :- use_module(library(clpfd), [transpose/2]).
    
    latin_square(N, S) :-
        numlist(1, N, Row),
        length(Rows, N),
        maplist(copy_term(Row), Rows),
        maplist(permutation, Rows, S),
        transpose(S, T),
        maplist(valid, T).
    
    valid([X|T]) :-
        memberchk(X, T), !, fail.
    valid([_|T]) :- valid(T).
    valid([_]).
    

    test:

    ?- aggregate(count,S^latin_square(4,S),C).
    C = 576.
    

    edit your code, once corrected removing typos, it’s a verifier, not a generator, but (as noted by ssBarBee in a deleted comment), it’s flawed by missing test on not adjacent rows.
    Here the corrected code

    delete(X, [X|T], T).
    delete(X, [H|T], [H|S]) :-
        delete(X, T, S).
    
    permutation([], []).
    permutation([H|T], R):-
        permutation(T, X),
        delete(H, R, X).
    
    latinSquare([_]).
    latinSquare([A,B|T]) :-
        permutation(A,B),
        isSafe(A,B),
        latinSquare([B|T]).
    
    isSafe([], []).
    isSafe([H1|T1], [H2|T2]) :-
        H1 =\= H2,
        isSafe(T1, T2).
    

    and some test

    ?- latinSquare([[1,2,3],[2,3,1],[3,2,1]]).
    false.
    
    ?- latinSquare([[1,2,3],[2,3,1],[3,1,2]]).
    true .
    
    ?- latinSquare([[1,2,3],[2,3,1],[1,2,3]]).
    true .
    

    note the last test it’s wrong, should give false instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a prolog program that can find the tiles that
now, i'm trying to write program using php and sqlite, but i have problem.
I was trying this palindrome program in prolog, the logic works but write operation
I'm trying to write a program in Prolog, which will insert an element into
I'm trying to write a program with Hibernate. My domain is now complete and
I am trying to write a Prolog program that will print out the male
I'm trying to write a program to automate a ticket draft. We have a
I am trying to write a program in java where in i can find
I am trying to write a program to find the largest prime factor of
I'm trying to write a prolog program that behaves in the following fashion: it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.