Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.30 KB | None | 0 0
  1. unique([]):-!.
  2. unique([H|T]):-
  3.     member(H, T), !, fail;
  4.     unique(T).
  5. getLen([],0).
  6. getLen([_|T],N) :- getLen(T,N1), N is N1+1.
  7. pred(_,0,[]):-!.
  8. pred([H|Tail],N,[H|NewTail]):-N1 is N-1,pred(Tail,N1,NewTail).
  9. pred([_|Tail],N,Ans):-pred(Tail,N,Ans).
  10. point([X,Y]) :-
  11.              random_between(0,50,X),
  12.              random_between(0,50,Y).
  13. checkP([X1,Y1],[X2,Y2],[X3,Y3]) :-
  14.                                 XY1 is X2-X1, XY2 is Y2-Y1,
  15.                                 XZ1 is X3-X1, XZ2 is Y3-Y1,
  16.                                 XZ1 \= 0, XZ2 \= 0,
  17.                                 A is XY1 / XZ1, B is XY2 / XZ2,
  18.                                 A\=B.
  19. showt([],_):-!.
  20. showt([H|T],N) :-
  21.                write('Triangle №'),write(N),
  22.                write(' '),write(H),nl,
  23.                N1 is N+1,
  24.                showt(T,N1).
  25. res(L,S) :-
  26.          showt(L,1),write('Square : '),
  27.          _s is round(S),write(_s),nl.
  28. sortL(L1,L2) :-
  29.              pred(L1,2,L2),
  30.              shared(L2).
  31. shared([[A,B,C],[D,E,F]]) :-
  32.                          L= [A,B,C,D,E,F],
  33.                          unique(L).
  34. best([],L,L0,S0,S) :-!,L=L0,S=S0.
  35. best([H|T],L,_,S0,S) :-
  36.                 square(H,S1),
  37.                 S1<S0,!,best(T,L,H,S0,S).
  38. best([_|T],L,L0,S0,S) :- best(T,L,L0,S0,S).
  39.  
  40. square([],0) :-!.
  41. square([H|T],S) :-
  42.                 square(T,S1),
  43.                 geron(H,Temp),
  44.                 S is S1 + Temp.
  45. geron([P1,P2,P3],S) :-
  46.                     P1 = [X1,Y1],P2 = [X2,Y2],P3 = [X3,Y3],
  47.                     Ax is X1 -X2, Ay is Y1 - Y2,
  48.                     Axy is Ax*Ax + Ay*Ay, sqrt(Axy,A),
  49.                     Bx is X1 - X3, By is Y1 -Y3,
  50.                     Bxy is Bx*Bx + By*By, sqrt(Bxy,B),
  51.                     Cx is X2 - X3, Cy is Y2 - Y3,
  52.                     Cxy is Cx*Cx +Cy*Cy, sqrt(Cxy,C),
  53.                     P is (A + B + C)/2,
  54.                     St is P*(P-A)*(P-B)*(P-C),
  55.                     sqrt(St,S).
  56. gen(0,[]):-!.
  57. gen(N,[H|L]) :-
  58.          point(H),
  59.          N1 is N - 1,
  60.          gen(N1,L).
  61. run :-
  62.     gen(6,L),
  63.     unique(L),
  64.     write('Points : '),write(L),nl,
  65.     findall(L1,pred(L,3,L1),List),
  66. %     showt(List,1),
  67.     findall(L2,sortL(List,L2),L3),
  68.     showt(L3,1),
  69.     L3 = [H|T],
  70.     square(H,S0),
  71.     best(T,L4,H,S0,S),
  72.     res(L4,S).
  73. solve :-
  74.       run;solve.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement