Untitled

 avatar
unknown
prolog
5 months ago
2.6 kB
10
Indexable
% Helper predicate to check that there are no common elements between two lists
disjoint([], _).
disjoint([H|T], L) :-
    \+ member(H, L),
    disjoint(T, L).

% Main generator3 predicate with conditions to ensure neither list is empty
generator3(AS, BS) :-
    permutation([1,2,3,4,5,6,7,8,9], Combined),
    append(AS, BS, Combined),
    disjoint(AS, BS),
    AS \= [],   % Ensure AS is not empty
    BS \= [].   % Ensure BS is not empty

% Base case: an empty list corresponds to 0.
construct_number([], 0).

% Recursive case: process each digit from left to right in the reversed list.
construct_number([D|Ds], N) :-
    construct_number(Ds, N1),
    N is N1 * 10 + D.

number(Digits, N) :-
    reverse(Digits, ReversedDigits), 
   construct_number(ReversedDigits, N).


% Check if a number is a palindrome
is_palindrome(Number) :-
    number_codes(Number, Codes),
    reverse(Codes, Codes).

% Main selector3 predicate
selector3(AS, BS) :-
    number(AS, A),
    number(BS, B),
    A \= B,
    % Ensure A is the smaller number
    (A < B -> Smallest = A ; Smallest = B),
    % Check that the last digit of the smallest is 3
    Smallest mod 10 =:= 3,
    % Check product palindrome condition
    Product is A * B,
    is_palindrome(Product),
    number_codes(Product, [52|_]),  % Ensure Product starts with '4'
    % Check sum palindrome condition
    Sum is A + B + 100,
    is_palindrome(Sum).

% x_unit/3 - Increments count if the test condition succeeds or fails as expected and prints results
x_unit(Test, N, N1) :-
    (   Test
    ->  N1 is N + 1,
        write('Test passed: '), write(Test), nl
    ;   N1 = N,
        write('Test failed: '), write(Test), nl
    ).


% Generate pairs and apply selector3 to filter valid ones
test_selector3(AS, BS) :-
    generator3(AS, BS),
    selector3(AS, BS).


% x_selector3 testing predicate with corrected calls to selector3
x_selector3(N10) :-
    x_unit(selector3([1,7,6,9,5,4,8], [2,3]), 0, N1),
    x_unit(selector3([2,3], [1,7,6,9,5,4,8]), N1, N2),
    x_unit(\+ selector3([1,7], [9,6,4,8,3,5,2]), N2, N3),
    x_unit(\+ selector3([1,7], [4,2,8,3,5,2,6]), N3, N4),
    x_unit(\+ selector3([1,7,6,9,5,2,8], [4,3]), N4, N5),
    x_unit(\+ selector3([], [6,9,5,2,8,4,3,1,7]), N5, N6),
    x_unit(\+ selector3([5,3,1,4,9], [8,2,6,7]), N6, N7),
    x_unit(\+ selector3([9,1,5,3,4], [7,6,8,2]), N7, N8),
    x_unit(\+ selector3([6,2,8,9], [5,4,3,1,7]), N8, N9),
    x_unit(\+ selector3([9,7,3,8,2,1,4,6,5], []), N9, N10).
Editor is loading...
Leave a Comment