Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(combinat)
- seated <- 10
- perms <- matrix(unlist(permn(seated-1)), ncol = seated-1, byrow = TRUE)
- permsextended <- cbind(1, perms+1, 1)
- pairs <- 100 * permsextended[,-(seated+1)] + permsextended[,-1]
- originalpairs <- c(100*(1:(seated-1)) + (2:seated), 100*seated + 1,
- 100*(2:seated) + (1:(seated-1)), 100*1 + seated)
- dupes <- matrix(pairs %in% originalpairs, ncol=seated)
- totaldupes <- rowSums(dupes)
- nodupes <- permsextended[totaldupes==0, -(seated+1)]
- > nrow(nodupes)
- [1] 29926
- > nrow(nodupes) / factorial(seated-1)
- [1] 0.08246803
- > head(nodupes)
- [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
- [1,] 1 7 2 9 3 10 4 6 8 5
- [2,] 1 7 2 10 3 9 4 6 8 5
- [3,] 1 7 2 9 3 8 4 6 10 5
- [4,] 1 7 2 10 3 8 4 6 9 5
- [5,] 1 7 2 8 3 10 4 6 9 5
- [6,] 1 7 2 8 3 9 4 6 10 5
- > table(nodupes[,6])
- 2 3 4 5 6 7 8 9 10
- 4318 2844 3186 3048 3134 3048 3186 2844 4318
- set.seed(1)
- cases <- 1000000
- seated <- 10 # should be less than 100
- originalpairs <- c(100*(1:(seated-1)) + (2:seated), 100*seated + 1,
- 100*(2:seated) + (1:(seated-1)), 100*1 + seated)
- runningcount <- 0
- for (i in 1:cases){
- example <- sample(seated)
- examplextend <- c(example, example[1])
- examplepairs <- 100 * examplextend[-(seated+1)] + examplextend[-1]
- runningcount <- runningcount + (sum(examplepairs %in% originalpairs)==0)
- }
- > runningcount / cases
- [1] 0.082199
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement