PROLOG打印以7结尾的数字,并且其数字的总和大于100 [英] PROLOG Print numbers that end in 7 and the sum of its digits is greater than 100
本文介绍了PROLOG打印以7结尾的数字,并且其数字的总和大于100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要断言一个接收数字列表的谓词,并且仅输出以7结尾且其数字总和大于100的数字
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 for separated but I need help making a union of the two predicates, I mean that the two predicates go into one only predicate, this is what I did so far:
%sum of digits greater than 100
multi(X):-
0 is X mod 100
sum([],0).
sum([P|Q],Z).
multi(P), sum(Q,Z1), Z is P + Z1.
sum([P|Q],Z).
not multi(P), sum(Q,Z).
%print the numbers that end in 7
end(Y):-
7 is Y mod 10.
listend([],0).
listend([P|Q]):-
end(P),write(P), nl, listend(Q).
listend([P|Q]):-
not(end(P)), listend(Q).
推荐答案
这对我有用:
?- 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).
我得到:
[147, 57, 3667]
No.
我假设您的意思是数字的总和大于10,而不是100.
I assumed you meant that the sum of the digits was greater than 10, rather than 100.
这篇关于PROLOG打印以7结尾的数字,并且其数字的总和大于100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文