Skip to content

Commit

Permalink
Trying gtools package
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazendaaa committed Nov 27, 2024
1 parent f15c3b8 commit dfa8227
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 155 deletions.
2 changes: 1 addition & 1 deletion src/r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export(problem8)
export(zip)
importFrom(Rcpp,sourceCpp)
importFrom(assertthat,is.count)
importFrom(combinat,permn)
importFrom(gtools,permutations)
importFrom(readr,read_file)
useDynLib(projectEuler)
74 changes: 8 additions & 66 deletions src/r/R/problem-24.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,18 @@
#
# Answer: 2783915460

permutations <- function(interval) {
if (1 == length(interval)) {
return (matrix(interval))
}

pivot <- interval[1]
result <- permutations(interval[-1])
newMatrix <- matrix(nrow = (nrow(result) - 1) + 2, ncol = ncol(result) + 1)
totalLines <- nrow(newMatrix)
totalColumns <- ncol(newMatrix)
middlePoint <- ceiling(totalLines / 2)
line <- 0
divisionPoint <- 0
firstPart <- c()
secondPart <- c()

print('0/2')
print(middlePoint)

while (line < middlePoint) {
line <- line + 1
divisionPoint <- line - 1
print('divisionPoint')
print(divisionPoint)
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)
}

print('1/2')
print(newMatrix)

while (line < totalLines) {
divisionPoint <- totalColumns - line
firstPart <- result[ line, 1 : divisionPoint ]
secondPart <- if(1 < divisionPoint) result[ line, divisionPoint : ncol(result) ] else c()
line <- line + 1
newMatrix[line, ] <- c(firstPart, pivot, secondPart)
}

print('2/2')
print(newMatrix)

return (newMatrix)
#'
#' @importFrom gtools permutations
#'
generateLexicographicPermutations <- function(interval) {
return (permutations(n = length(interval), r = length(interval), v = interval))
}

#'
#' @importFrom combinat permn
#'
#' @export
#'
problem24 <- function(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
}
problem24 <- function(interval, index) {
result <- generateLexicographicPermutations(interval)

return (sort(all))
return (paste(result[index, ], collapse = ""))
}
Loading

0 comments on commit dfa8227

Please sign in to comment.