Вы находитесь на странице: 1из 15

-

%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%

..............................................................
............................................
..............................................................
..................................................
N- .......................................................
..........................................................
( rev) ....................................................
............................
/ ( ) ...............
, ...............................
.........................................
, ..........................
? ( conc, ) ..................................
.....................
( ) .............................
..............................................

4
4
4
4
5
5
5
5
6
6
6
6
7
7
7
7

%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%

......................................................... 8
...................... 8
........................... 8
.................................................... 8
........................................... 9
, ................................................ 9
, , 0 ............... 9
................................................... 10
....................................................... 10
.............................................. 11
.................................................. 11
( -) .......... 11
.................................. 11
................................................ 11
? ( gcd ) ........................................... 12
......................... 12
............................................................ 14
.............................................. 14

% ..................................... 15

%
conc([ ], L, L).
conc([H|T], L, [H|T1]) :- conc(T,L,T1).
%
member(X,[X|_]).
member(X,[_|T]) :- member(X,T).
%
length([], 0).
length([_|T], L) :- length(T, L_T), L = L_T + 1.
%
last2([X],X).
last2([_|L],X) :- last2(L,X).

% N-
n_element([X|_],1,X).
n_element([_|L],N,Y) :- N1 is N-1, n_element(L,N1,Y).
%
rev([H|T],L1,L2) :- rev(T,[H|L1],L2).
rev([ ],L,L).
reverse2(L1,L2) :- rev (L1,[ ],L2).
% ( rev)
palindrom(L) :- rev(L,L).
%
add_if_new(X,L,_):-member(X,L),!.
add_if_new(X,L,[X|L]).

% / ( )
lshift([H|L], S) :- append(L, [H], S).
rshift(L, S) :- lshift(S, L).
% ,
insert(X,[],[X]).
insert(X,[Y|Z],[X,Y|Z]) :- X < Y.
insert(X,[Y|Z],[Y|R]) :- Y < X, insert(X,Z,R).
%
delete_one(_,[],[]).
delete_one(X,[X|L],L):- !.
delete_one(X,[Y|L],[Y|L1]):- delete_one(X,L,L1).
% ,
last(L,X) :- onc(_,[X],L).

% ? ( conc, )
neighbors2(X,Y,L) :- conc(_,[X,Y|_],L); conc(_,[Y,X|_],L).
%
delete_all(_,[],[]).
delete_all(X,[X|L],L1) :- delete_all (X,L,L1).
delete_all (X,[Y|L],[Y|L1]) :- X<>Y, delete_all (X,L,L1).
% ( )
sumar([],0).
sumar([H|T],V):-sumar(T,W),V is W+H.
%
last_but_one(X,[X,_]).
last_but_one(X,[_|Ys]) :- last_but_one(X,Ys).

%
my_flatten(X,[X]) :- \+ is_list(X).
my_flatten([],[]).
my_flatten([X|Xs],Zs) :- my_flatten(X,Y), my_flatten(Xs,Ys), append(Y,Ys,Zs).
%
compress([],[]).
compress([X],[X]).
compress([X,X|Xs],Zs) :- compress([X|Xs],Zs).
compress([X,Y|Ys],[X|Zs]) :- X \= Y, compress([Y|Ys],Zs).
%
max2(X,Y,X) :- X>Y,!.
max2(_,Y,Y).
%
max3(X,Y,Z,M) :- max2(X,Y,XY), max2(XY,Z,M).

%
fact2(N,F,N,F) :- !.
fact2(N,F,N1,F1) :- N2 is N1+1, F2 is F1*N2, fact2(N,F,N2,F2).
factM(N,F) :- fact2(N,F,1,1).
% ,
fib(0,1):-!.
fib(1,1):-!.
fib(N,F):- N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2.
% , , 0
fib_fast(1,1,1):-!.
fib_fast(N,FN,FN1) :- N1 is N-1,fib_fast(N1,FN_1,FN), FN1 is FN+FN_1.
fib_fast(N,FN):- fib_fast(N,FN,_).

%
bubblesort(List,Sorted) :- swap(List,List1),!, bubblesort(List1,Sorted).
bubblesort(Sorted,Sorted).
swap([X,Y|Rest], [Y,X|Rest]) :- X > Y.
swap([Z|Rest],[Z|Rest1]) :- swap(Rest,Rest1).
%
s(_,[],[],[]).
s(X, [H|T],[H|Ls],Gt):-H<X,!,s(X,T,Ls,Gt).
s(X, [H|T],Ls,[H|Gt]):-s(X,T,Ls,Gt).
conc([],B,B).
conc([H|A],R,[H|B]):-conc(A,R,B).
qs([],[]).
qs([X|T],R):-s(X,T,Ls,Gt),qs(Ls,SLs),qs(Gt,SGt),conc(SLs,[X|SGt],R).

10

%
hyp(A,B,C) :- sqrt(A*A+B*B,C).
%
abs2(X,Y) :- max2(X,-X,Y).
% ( -)
f2(X,0):-X<3,!.
f2(X,2):-X<6,!.
f2(_,4).
%
avg([],0):-!.
avg(L,A) :- summa(L,S), length(L,K), A=S/K.
%
gcd(X,0,X) :- X > 0.
gcd(X,Y,G) :- Y > 0, Z is X mod Y, gcd(Y,Z,G).
11

% ? ( gcd )
coprime(X,Y) :- gcd(X,Y,1).
%
% pack(L1,L2) :- L2 L1
%
pack([],[]).
pack([X|Xs],[Z|Zs]) :- transfer(X,Xs,Ys,Z), pack(Ys,Zs).
% transfer(X,Xs,Ys,Z) Ys , Xs
% , X Z
transfer(X,[],[],[X]).
transfer(X,[Y|Ys],[Y|Ys],[X]) :- X \= Y.
transfer(X,[X|Xs],Ys,[X|Zs]) :- transfer(X,Xs,Ys,Zs).
:
?- pack([a,a,a,a,b,c,c,a,a,d,e,e,e],X).
X = [[a, a, a, a], [b], [c, c], [a, a], [d], [e, e, e]] .

12

%
encode(L1,L2) :- pack(L1,L), transform(L,L2).
transform([],[]).
transform([[X|Xs]|Ys],[[N,X]|Zs]) :- length([X|Xs],N), transform(Ys,Zs).
:
?- encode([a,a,a,a,b,c,c,a,a,d,e,e,e],X).
X = [[4, a], [1, b], [2, c], [2, a], [1, d], [3, e]] .

13

%
is_prime(2).
is_prime(3).
is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+ has_factor(P,3).
has_factor(N,L) :- N mod L =:= 0.
has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(N,L2).
%
prime(X):-not(has_factor(X)).
has_factor(X):-has_factor(X,2).
has_factor(X,Factor):-X mod Factor =:= 0,!.
has_factor(X,Factor):-Y is Factor+1,Y*Y=<X,has_factor(X,Y).

14

%
prime_factors(N,L) :- N > 0, prime_factors(N,L,2).
% prime_factors(N,L,K) :- L is the list of prime factors of N. It is
% known that N does not have any prime factors less than K.
prime_factors(1,[],_) :- !.
prime_factors(N,[F|L],F) :- % N is multiple of F
R is N // F, N =:= R * F, !, prime_factors(R,L,F).
prime_factors(N,L,F) :next_factor(N,F,NF), prime_factors(N,L,NF).
% next_factor(N,F,NF) :- when calculating the prime factors of N
% and if F does not divide N then NF is the next larger candidate to
% be a factor of N.
next_factor(_,2,3) :- !.
next_factor(N,F,NF) :- F * F < N, !, NF is F + 2.
next_factor(N,_,N). % F > sqrt(N)

?- prime_factors(111384,L).
L = [2, 2, 2, 3, 3, 7, 13, 17].
15