Advertisement
bojandam1

Cas 19.10.24 : fiibonaci, flatten

Oct 19th, 2024
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.42 KB | None | 0 0
  1. %fibonacii(1,1):-!.
  2. %fibonacii(N,R):- fibonacii(N,R,[1,1],2).
  3. %fibonacii(N,R,[R|_],N):-!.
  4. %fibonacii(N,R,[X1,X2|Ost],X):-N>X,Xp1 is X+1,B is X1+X2,fibonacii(N,R,[B,X1,X2|Ost],Xp1).
  5.  
  6. %Pologicko resenie
  7. fibonacii(1,1):-!.
  8. fibonacii(N,R):- fibonacii(N,R,1,1,2).
  9. fibonacii(N,R,R,_,N):-!.
  10. fibonacii(N,R,X1,X2,X):-N>X,Xp1 is X+1,B is X1+X2,fibonacii(N,R,B,X1,Xp1).
  11.  
  12. % sekogas e lista od listi [[a,b,c],[d],[e,f],...]
  13. izramni_simple([L],L):-!.
  14. izramni_simple([Pl|Ol],R):- izramni_simple(Ol,Rr),merge(Pl,Rr,R).
  15.  
  16. e_lista([]).
  17. e_lista([_|_]).
  18.  
  19. merge([],L2,L2):-!.
  20. merge([X|Ost],L2,[X|Ost1]):-merge(Ost,L2,Ost1).
  21.  
  22. izramni([],[]).
  23. izramni([Prv_clen|Ol],R):-
  24.     izramni(Ol,Rr),
  25.     (
  26.         e_lista(Prv_clen),
  27.         izramni(Prv_clen,Rr2),
  28.         merge(Rr2,Rr,R),!
  29.         ;  
  30.         R=[Prv_clen|Rr]
  31.     ).
  32. test_izramni:-
  33.     izramni([1,2,3],[1,2,3]),
  34.     izramni([[[[1]]],2,3,[4,[5,[6],7,[8,9,[10,11],[12]]],13],14],[1,2,3,4,5,6,7,8,9,10,11,12,13,14]),
  35.     not(izramni([[[[1]]],2,3,[4,[5,[6],7,[8,9,[10,11],[12]]],13],14],[1,2,3,4,5,6,7,8,9,10,11,12,11,14])).
  36. test_fib :-
  37.     fibonacii(1,1),
  38.     fibonacii(2,1),
  39.     fibonacii(3,2),
  40.     fibonacii(4,3),
  41.     not(fibonacii(1,2)),
  42.     not(fibonacii(2,2)),
  43.     not(fibonacii(3,3)),
  44.     not(fibonacii(4,5)),
  45.     not(fibonacii(3,1)),
  46.     not(fibonacii(4,2)),
  47.     fibonacii(5,5),
  48.     fibonacii(6,8),
  49.     fibonacii(33 , 3524578),
  50.     fibonacii(40 , 102334155),
  51.     fibonacii(27 , 196418).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement