PROLOG Print numbers that end in 7 and the sum of its digits is greater than 100

前端 未结 2 479
滥情空心
滥情空心 2021-01-22 19:45

I need to make a predicate that receives a numeric list and print only the numbers that end in 7 and that the sum of its digits is greater than 100

I made the predicates

2条回答
  •  终归单人心
    2021-01-22 20:09

    This works for me:

    ?- filter([147, 24, 57, 17, 3667], X), write(X), nl, fail.
    
    sumdigits(0, 0).
    sumdigits(X, Z) :-
        X > 0,
        Z1 is X mod 10, 
        X2 is X // 10,
        sumdigits(X2, Z2), 
        Z is Z1 + Z2.
    
    filter([], []).
    filter([H|X], [H|Y]) :-
        sumdigits(H, D),
        D > 10,
        7 is H mod 10, !,
        filter(X, Y).
    filter([_|X], Y) :- filter(X, Y).
    

    I get:

    [147, 57, 3667]
    No.
    

    I assumed you meant that the sum of the digits was greater than 10, rather than 100.

提交回复
热议问题