How to create all 4-digit code from 6 numbers and without using duplicates?

I am trying to create a list containing all possible 4-digit codes using numbers from 1 to 6. I cannot use duplicates, so it is [1, 2, 3, 3]not allowed.

I came up with the idea to first create all 1296 possible WITH codes using duplicates, and then filter out every code that does not match the rule: no duplicates. To do this, I will need to first create these 1296 codes. So, starting with [1, 1, 1, 1]and ending with [6, 6, 6, 6].

Can someone help me? I know what he should do [1, 1, 1, 1], and then [1, 1, 1, 2]etc. But I don’t know how to implement this.

I forgot to mention: I started by creating one code that followed the rules. This is also not so good. This was my code:

single_code([]).
single_code(Code1):-
  length(Code1, Y),
  Y < 4,
  random_between(1, 6, X),
  \+member(X, Code1),
  single_code([X|Code1]).
single_code(Code1):-
  single_code(Code1).

, , . - , , .

+4
1

- member/2. member/2 , : (, member(X,[1,4,2,5])) , (, member(2,L)).

. , member(A,[1,2,3,4,5,6]). X 1, 2,..., 6.

, :

four([A,B,C,D]) :-
    member(A,[1,2,3,4,5,6]),
    member(B,[1,2,3,4,5,6]),
    member(C,[1,2,3,4,5,6]),
    member(D,[1,2,3,4,5,6]).

backtracing . , eBlements [1,2,3,4,5,6].

, , , [2,2,3,3], . A \= B , , A B. , , :

four([A,B,C,D]) :-
    member(A,[1,2,3,4,5,6]),
    member(B,[1,2,3,4,5,6]),
    member(C,[1,2,3,4,5,6]),
    member(D,[1,2,3,4,5,6]),
    A \= B,
    A \= C,
    A \= D,
    B \= C,
    B \= D,
    C \= D.

: , , , . , , A B , , C D, . , :

four([A,B,C,D]) :-
    member(A,[1,2,3,4,5,6]),
    member(B,[1,2,3,4,5,6]),
    A \= B,
    member(C,[1,2,3,4,5,6]),
    A \= C,
    B \= C,
    member(D,[1,2,3,4,5,6]),
    A \= D,
    B \= D,
    C \= D.

, : . , , - . , .

, , [1..n]. :

between(A,B,A) :-
    A =< B.
between(A,B,C) :-
    A < B,
    A1 is A+1,
    between(A1,B,C).

between(1,6,X)., X 1, 2, 3, 4, 5 6.

, :

distinct_n(A,B,N,L) :-
    distinct_n(A,B,N,[],L).

distinct_n(_,_,0,_,[]).
distinct_n(A,B,N,Xs,[X|R]) :-
    N > 0,
    between(A,B,X),
    \+ member(X,Xs),
    N1 is N-1,
    distinct_n(A,B,N1,[X|Xs],R).

, L N A B ( ), distinct_n(A,B,N,L)..

3- 1-4, :

?- distinct_n(1,4,3,L).
L = [1, 2, 3] ;
L = [1, 2, 4] ;
L = [1, 3, 2] ;
L = [1, 3, 4] ;
L = [1, 4, 2] ;
L = [1, 4, 3] ;
L = [2, 1, 3] ;
L = [2, 1, 4] ;
L = [2, 3, 1] ;
L = [2, 3, 4] ;
L = [2, 4, 1] ;
L = [2, 4, 3] ;
L = [3, 1, 2] ;
L = [3, 1, 4] ;
L = [3, 2, 1] ;
L = [3, 2, 4] ;
L = [3, 4, 1] ;
L = [3, 4, 2] ;
L = [4, 1, 2] ;
L = [4, 1, 3] ;
L = [4, 2, 1] ;
L = [4, 2, 3] ;
L = [4, 3, 1] ;
L = [4, 3, 2] ;
false.

, findall/3:

?- findall(L,distinct_n(1,4,3,L),LL).
LL = [[1, 2, 3], [1, 2, 4], [1, 3, 2], [1, 3, 4], [1, 4, 2], [1, 4, 3], [2, 1, 3], [2, 1, 4], [2, 3, 1], [2, 3, 4], [2, 4, 1], [2, 4, 3], [3, 1, 2], [3, 1, 4], [3, 2, 1], [3, 2, 4], [3, 4, 1], [3, 4, 2], [4, 1, 2], [4, 1, 3], [4, 2, 1], [4, 2, 3], [4, 3, 1], [4, 3, 2]].

CLP (FD)

Prolog C onstraint L ogic P rogramming F inite D omains (clpfd). , :

:- use_module(library(clpfd)).

distinct_n(A,B,N,L) :-
    length(L,N),
    L ins A..B,
    all_distinct(L),
    label(L).

Se , L N. L A..B, all_distinct/1. , label/1, .

+3

Source: https://habr.com/ru/post/1686744/


All Articles