this program calculates the binary of any number. all you must do is write binario(7,X) for example.
% |0 se x<y
% div(x,y)|
% |1+div(x-y,y) se x>=y
% div(4,2,X).
div(X,Y,0):-X<Y.
div(X,Y,Z):-X>=Y,
X1 is X-Y,
div(X1,Y,Z1),Z is 1+Z1.
%calculates the quotients of a divisao
% lstQuo(120,2,L).
lstQuo(X,Y,[]):-X < Y.
lstQuo(X,Y,R):-X >= Y,
div(X,Y,Z),add(X,L,R),lstQuo(Z,Y,L).
%rest of the divisao of natural numbers with successives subtractions
% |x se x<y
% mod(x,y)|
% |mod(x-y,y) se x>=y
%resto(234,24,X).
resto(X,Y,X):-X<Y.
resto(X,Y,Z):-X>=Y,
X1 is X-Y,
resto(X1,Y,Z).
%calculates the remains of a list
% lstResto([1,2,3,4,5,6],[2],S).
lstResto([],_,[]).
lstResto([E|T],R,[N|S]):-resto(E,R,N),lstResto(T,R,S).
%insert in the begining of the list
add(X,L,[X|L]).
%calculates the binary number
% binario(7,S).
binario(0,[0]):-!.
binario(1,[0,1]):-!.
binario(X,R):-lstQuo(X,2,L),lstResto(L,2,S),reverse(S,B),add(1,B,R).