I can check if X is a ancestor of Y and can count how many descendants X has I don't know how to list all people that has a given number of descendants. N = COUNT does not work when I put it in the rule.
parent(a,b).
parent(a,d).
parent(b,c).
parent(c,e).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
?- aggregate_all(count, ancestor(a,_), COUNT). % output: COUNT = 4
?- aggregate_all(count, ancestor(b,_), COUNT). % output: COUNT = 2
?- aggregate_all(count, ancestor(c,_), COUNT). % output: COUNT = 1
?- aggregate_all(count, ancestor(d,_), COUNT). % output: COUNT = 0
?- aggregate_all(count, ancestor(e,_), COUNT). % output: COUNT = 0
% List all X that have N descendants
list_people(X,N) :-