fork(2) download
  1. :- use_module(library(clpfd)).
  2.  
  3. permut(L,N) :-
  4. length(L, N),
  5. L ins 1..N,
  6. all_different(L),
  7. label(L).
  8.  
  9. noFixpoint([],_Xs).
  10. noFixpoint([X|Xs],N1) :-
  11. X #\= N,
  12. N1 #= N-1,
  13. noFixpoint(Xs,N).
  14.  
  15. derangement(L,N) :-
  16. permut(L,N),
  17. noFixpoint(L,0).
  18.  
  19. findDerangements(N,Ls) :-
  20. findall(L,derangement(L,N),Ls).
  21.  
  22. :- findDerangements(4,Ls), length(Ls,N), print(Ls), nl, print(N), nl, nl.
  23. :- findDerangements(5,Ls), length(Ls,N), print(Ls), nl, print(N).
Success #stdin #stdout #stderr 0.21s 7544KB
stdin
Standard input is empty
stdout
[[2,1,4,3],[2,3,4,1],[2,4,1,3],[3,1,4,2],[3,4,1,2],[3,4,2,1],[4,1,2,3],[4,3,1,2],[4,3,2,1]]
9

[[2,1,4,5,3],[2,1,5,3,4],[2,3,1,5,4],[2,3,4,5,1],[2,3,5,1,4],[2,4,1,5,3],[2,4,5,1,3],[2,4,5,3,1],[2,5,1,3,4],[2,5,4,1,3],[2,5,4,3,1],[3,1,2,5,4],[3,1,4,5,2],[3,1,5,2,4],[3,4,1,5,2],[3,4,2,5,1],[3,4,5,1,2],[3,4,5,2,1],[3,5,1,2,4],[3,5,2,1,4],[3,5,4,1,2],[3,5,4,2,1],[4,1,2,5,3],[4,1,5,2,3],[4,1,5,3,2],[4,3,1,5,2],[4,3,2,5,1],[4,3,5,1,2],[4,3,5,2,1],[4,5,1,2,3],[4,5,1,3,2],[4,5,2,1,3],[4,5,2,3,1],[5,1,2,3,4],[5,1,4,2,3],[5,1,4,3,2],[5,3,1,2,4],[5,3,2,1,4],[5,3,4,1,2],[5,3,4,2,1],[5,4,1,2,3],[5,4,1,3,2],[5,4,2,1,3],[5,4,2,3,1]]
44
stderr
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit