So I'm trying to work out in Prolog how to take an input, and return whether it is a perfect number or not. Currently i'm close but can't figure out why it isn't working the way I want. I am approaching this through recursion. If X %N == 0, then N and TT(total) is added to TT1. N will decrease by one and the function will be called recursively until it reaches 1, then the total will be compared to the input number X. My code is:
factors(X,N,TT) :-
( 0 is X mod N -> TT1 is N + TT ),
TT = TT1,
( N > 1 -> N1 is N-1, factors(X, N1, TT1)
; TT1 = X
).
perfect(X) :-
factors(X, X-1, 0).
Currently it is returning that all inputs are false. I hope this isn't majorly incorrect and only needs to be tweaked.
Cheers