Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. library(combinat)
  2. seated <- 10
  3. perms <- matrix(unlist(permn(seated-1)), ncol = seated-1, byrow = TRUE)
  4. permsextended <- cbind(1, perms+1, 1)
  5. pairs <- 100 * permsextended[,-(seated+1)] + permsextended[,-1]
  6. originalpairs <- c(100*(1:(seated-1)) + (2:seated), 100*seated + 1,
  7. 100*(2:seated) + (1:(seated-1)), 100*1 + seated)
  8. dupes <- matrix(pairs %in% originalpairs, ncol=seated)
  9. totaldupes <- rowSums(dupes)
  10. nodupes <- permsextended[totaldupes==0, -(seated+1)]
  11.  
  12. > nrow(nodupes)
  13. [1] 29926
  14. > nrow(nodupes) / factorial(seated-1)
  15. [1] 0.08246803
  16.  
  17. > head(nodupes)
  18. [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
  19. [1,] 1 7 2 9 3 10 4 6 8 5
  20. [2,] 1 7 2 10 3 9 4 6 8 5
  21. [3,] 1 7 2 9 3 8 4 6 10 5
  22. [4,] 1 7 2 10 3 8 4 6 9 5
  23. [5,] 1 7 2 8 3 10 4 6 9 5
  24. [6,] 1 7 2 8 3 9 4 6 10 5
  25.  
  26. > table(nodupes[,6])
  27. 2 3 4 5 6 7 8 9 10
  28. 4318 2844 3186 3048 3134 3048 3186 2844 4318
  29.  
  30. set.seed(1)
  31. cases <- 1000000
  32. seated <- 10 # should be less than 100
  33. originalpairs <- c(100*(1:(seated-1)) + (2:seated), 100*seated + 1,
  34. 100*(2:seated) + (1:(seated-1)), 100*1 + seated)
  35. runningcount <- 0
  36. for (i in 1:cases){
  37. example <- sample(seated)
  38. examplextend <- c(example, example[1])
  39. examplepairs <- 100 * examplextend[-(seated+1)] + examplextend[-1]
  40. runningcount <- runningcount + (sum(examplepairs %in% originalpairs)==0)
  41. }
  42.  
  43. > runningcount / cases
  44. [1] 0.082199
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement