このディレクトリの索引 # I'm working on a predicate which will count the number of divisors for a given number. It won't count all of them, but enough for me to know that it has more than two sets of factors. Here is what I have: # # countFactors([_,_,_,_X]):-!. # countFactors(Product, Count,Divisor, _X):- # Divisor =< Product/2, # Product mod Divisor = 0, # NewC is Count + 1, # NextD is Divisor + 1, # countFactors(Product,NewC, NextD, NewC). # However, running countFactors(16,0,2,X). Simply returns false, whereas i would expect it to return X = 2 # # EDIT: Ok, so now I realise why it returns false: it works fine if the divisor in question is a factor, and recurses. However, if it isn't a factor, then it gives false, but doesn't increment to the next divisor, it just stops and returns false. # # So my question is, how can I correct this? countFactors(Product, Count,Divisor, _X):- Divisor =< Product // 2, M is Product mod Divisor, NextD is Divisor + 1, newC(Count, M, NewC), countFactors(Product,NewC, NextD, _X). countFactors(Product,_X,Divisor,_X) :- Divisor > Product // 2. newC(Count, 0, NewC) :- NewC is Count + 1. newC(Count, M, Count) :- \+(M == 0).