lundi 30 mars 2020

How can I structure for and if loops to construct Latin squares?

My objective is to construct every unique Latin Square given a size n. The R command rlatin(n) uses a Markov chain to construct a random Latin Square of size n x n. However, this command won't just construct all Latin Squares of its size.

Below is my code:

L <- function(n){
  size <- factorial(n) * factorial(n-1)
  l <- list()
  l[[1]] <- rlatin(n)
  for(k in 2:size){
    new <- rlatin(n)
    for(j in 1:(k-1)){
      if(new == l[[j]]){
        new <- rlatin(n)
      }
    }
    l[[k]] <- new
  }
  l
} 

This is not working properly, and I cannot see why. Can someone please shed some light on my errors? Additionally, once all Latin Squares are constructed, is there a way that I can organize them so there is some clear in the Latin Squares?

Aucun commentaire:

Enregistrer un commentaire