Skip to content

Instantly share code, notes, and snippets.

@roag
Created March 6, 2009 16:07
Show Gist options
  • Save roag/74958 to your computer and use it in GitHub Desktop.
Save roag/74958 to your computer and use it in GitHub Desktop.
%%%6. Define a predicate list_of_divisors(N,L) which for a positive integer N
%%% calculates the list of its proper divisors. (A proper divisor of n is a positive
%%% integer m < n such that m divides n.)
divisor(N,P) :-
P>0,
N>P,
0 is N mod P.
list_divisors2(N,0,L).
list_divisors2(N,M,L):-
M>0,
P is M-1,
print(P),
print(L),
divisor(N,P),
list_divisors2(N,P,[P|L]).
list_divisors2(N,M,L):-
M>0,
P is M-1,
\+(divisor(N,P)),
list_divisors2(N,P,L).
list_divisors(N,L):-list_divisors2(N,N,L).
%%%4. Define a predicate insert3 that takes a list and produces another list in
%%%which break has been inserted at every third position. For example
%%%?- insert3([1,2,3,4,5,6],L).
%%%L = [1, 2, break, 3, 4, break, 5, 6, break] ;
insert([],Ys,N).
insert([X1|Xs],Ys,N):-
M is N+1,
0 is M mod 3,
insert(Xs,[X1,break|Ys],M).
insert([X1|Xs],Ys,N):-
M is N+1,
\+(0 is M mod 3),
insert(Xs,[X1|Ys],M).
insert3(X,Y):- insert(X,Y,0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment