I actually have task in school to find all prime factors of a given number and return them as a list, but I'm struggling a littlebit because I'm still new when it comes to prolog. Here is my code:
divisible(A, B) :-A mod B =:= 0, !.
divisible(A, B) :-A > B + 1, divisible(A, B + 1).
isPrime(2).
isPrime(A) :-not(divisible(A, 2)).
nextPrime(A, P) :-P is A + 1, isPrime(P),!.
nextPrime(A, P) :-A0 is A + 1,nextPrime(A0, P).
primeFactors(B,L):-primeFactors(B,L,2).
primeFactors(2,[2],_).
primeFactors(3,[3],_).
primeFactors(B,L,C):-B>3, C<B//2, B mod C =:= 0,add_tail(L,C,L1),B1 is B//C, primeFactors(B1,L1,C).
primeFactors(B,L,C):-B>3, C<B//2, nextPrime(C,C1),primeFactors(B,L,C1).
add_tail([],X,[X]).
add_tail([H|T],X,[H|L]):-add_tail(T,X,L).
Idea was to start with primeFactors/2, then from there to forward to primeFactors/3 where is C a prime divider, so it's easier for user to call it. B is given number, L is list that I want to return.
I should point out that isPrime, nextPrime and add_tail worked perfectly fine on their own.
I saw that there are some solutions for this problem on the internet but I still have only basic knowledge in prolog.