Skip to content

Commit

Permalink
Adding permn
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazendaaa committed Nov 23, 2024
1 parent 17b2306 commit f15c3b8
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 179 deletions.
1 change: 1 addition & 0 deletions src/r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export(problem8)
export(zip)
importFrom(Rcpp,sourceCpp)
importFrom(assertthat,is.count)
importFrom(combinat,permn)
importFrom(readr,read_file)
useDynLib(projectEuler)
21 changes: 17 additions & 4 deletions src/r/R/problem-24.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ permutations <- function(interval) {
firstPart <- if(0 < divisionPoint) result[ line, 1 : divisionPoint ] else c()
print('firstPart')
print(firstPart)
print('result[ line, ]')
print(result[ line, ])
secondPart <- result[ line, divisionPoint + 1 : ncol(result) ]
print('secondPart')
print(secondPart)
print('c(firstPart, pivot, secondPart)')
print(c(firstPart, pivot, secondPart))
newMatrix[line, ] <- c(firstPart, pivot, secondPart)
}

Expand All @@ -62,13 +66,22 @@ permutations <- function(interval) {
return (newMatrix)
}

permutations(0:2)

#'
#' @importFrom combinat permn
#'
#' @export
#'
problem24 <- function(interval) {
permutations <- permutations(interval)
#permutations <- permutations(interval)
#return (unlist(Map(function(x) paste(as.character(x), collapse=''), permutations)))
permutations <- permn(interval)
all <- vector(length = length(permutations))
index <- 1

for (option in permutations) {
all[index] <- as.integer(paste(as.character(option), collapse=''))
index <- index + 1
}

return (unlist(Map(function(x) paste(as.character(x), collapse=''), permutations)))
return (sort(all))
}
Loading

0 comments on commit f15c3b8

Please sign in to comment.