diff --git a/src/r/NAMESPACE b/src/r/NAMESPACE index 122e60d..9a7e6f8 100644 --- a/src/r/NAMESPACE +++ b/src/r/NAMESPACE @@ -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) diff --git a/src/r/R/problem-24.R b/src/r/R/problem-24.R index 928f53f..3af59b9 100644 --- a/src/r/R/problem-24.R +++ b/src/r/R/problem-24.R @@ -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 = "")) } \ No newline at end of file diff --git a/src/r/inst/excalidraw/problem-24.excalidraw b/src/r/inst/excalidraw/problem-24.excalidraw index 1813d0c..7bfe015 100644 --- a/src/r/inst/excalidraw/problem-24.excalidraw +++ b/src/r/inst/excalidraw/problem-24.excalidraw @@ -5,8 +5,8 @@ "elements": [ { "type": "text", - "version": 172, - "versionNonce": 1874178656, + "version": 173, + "versionNonce": 987426954, "isDeleted": false, "id": "OdHjIk5PMkWFmmWDVmTYM", "fillStyle": "solid", @@ -26,7 +26,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004135, + "updated": 1732662835796, "link": null, "locked": false, "fontSize": 20, @@ -41,8 +41,8 @@ }, { "type": "text", - "version": 202, - "versionNonce": 1434264480, + "version": 203, + "versionNonce": 686929174, "isDeleted": false, "id": "hVDyn2rc_7blldT2ms_Yf", "fillStyle": "solid", @@ -62,7 +62,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004136, + "updated": 1732662835796, "link": null, "locked": false, "fontSize": 20, @@ -77,8 +77,8 @@ }, { "type": "text", - "version": 203, - "versionNonce": 655386208, + "version": 204, + "versionNonce": 1610633034, "isDeleted": false, "id": "wOoW-NtlZda3826TyoMi0", "fillStyle": "solid", @@ -103,7 +103,7 @@ "type": "arrow" } ], - "updated": 1732316004137, + "updated": 1732662835796, "link": null, "locked": false, "fontSize": 20, @@ -118,8 +118,8 @@ }, { "type": "text", - "version": 280, - "versionNonce": 1566027680, + "version": 281, + "versionNonce": 1516675670, "isDeleted": false, "id": "qdXrXTnyJCq6sVt7GR6dD", "fillStyle": "solid", @@ -144,7 +144,7 @@ "type": "arrow" } ], - "updated": 1732316004137, + "updated": 1732662835796, "link": null, "locked": false, "fontSize": 20, @@ -255,8 +255,8 @@ }, { "type": "text", - "version": 288, - "versionNonce": 150248032, + "version": 289, + "versionNonce": 1586222602, "isDeleted": false, "id": "mLpoLzUkDh24KOpOxUTBe", "fillStyle": "solid", @@ -281,7 +281,7 @@ "type": "arrow" } ], - "updated": 1732316004137, + "updated": 1732662835797, "link": null, "locked": false, "fontSize": 20, @@ -344,8 +344,8 @@ }, { "type": "text", - "version": 311, - "versionNonce": 570473376, + "version": 312, + "versionNonce": 21751702, "isDeleted": false, "id": "IZLWzeyYtmJatciveLic2", "fillStyle": "solid", @@ -365,7 +365,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004137, + "updated": 1732662835797, "link": null, "locked": false, "fontSize": 20, @@ -380,8 +380,8 @@ }, { "type": "text", - "version": 220, - "versionNonce": 137514592, + "version": 221, + "versionNonce": 2004387018, "isDeleted": false, "id": "cRca8jSyjbf4p5AT4SCcn", "fillStyle": "solid", @@ -401,7 +401,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004138, + "updated": 1732662835797, "link": null, "locked": false, "fontSize": 20, @@ -416,8 +416,8 @@ }, { "type": "text", - "version": 250, - "versionNonce": 94914464, + "version": 251, + "versionNonce": 1037468886, "isDeleted": false, "id": "IpKBhhXH4bLsDcQUckK4B", "fillStyle": "solid", @@ -437,7 +437,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004138, + "updated": 1732662835797, "link": null, "locked": false, "fontSize": 20, @@ -452,8 +452,8 @@ }, { "type": "text", - "version": 251, - "versionNonce": 1033470560, + "version": 252, + "versionNonce": 341924746, "isDeleted": false, "id": "4P5op6STWRXYd64-FTj8D", "fillStyle": "solid", @@ -478,7 +478,7 @@ "type": "arrow" } ], - "updated": 1732316004138, + "updated": 1732662835798, "link": null, "locked": false, "fontSize": 20, @@ -493,8 +493,8 @@ }, { "type": "text", - "version": 328, - "versionNonce": 612258720, + "version": 329, + "versionNonce": 1637579286, "isDeleted": false, "id": "iIIt5wuCSkvJL4HGhVNqu", "fillStyle": "solid", @@ -519,7 +519,7 @@ "type": "arrow" } ], - "updated": 1732316004139, + "updated": 1732662835798, "link": null, "locked": false, "fontSize": 20, @@ -630,8 +630,8 @@ }, { "type": "text", - "version": 338, - "versionNonce": 670709344, + "version": 339, + "versionNonce": 864115274, "isDeleted": false, "id": "McmY1TCjkcgPaRcthLIHV", "fillStyle": "solid", @@ -656,7 +656,7 @@ "type": "arrow" } ], - "updated": 1732316004139, + "updated": 1732662835798, "link": null, "locked": false, "fontSize": 20, @@ -671,8 +671,8 @@ }, { "type": "text", - "version": 401, - "versionNonce": 347266976, + "version": 402, + "versionNonce": 1133496150, "isDeleted": false, "id": "xA0NE2jfT4hvqecHXjdqW", "fillStyle": "solid", @@ -692,7 +692,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004139, + "updated": 1732662835798, "link": null, "locked": false, "fontSize": 20, @@ -755,8 +755,8 @@ }, { "type": "text", - "version": 297, - "versionNonce": 1814325856, + "version": 298, + "versionNonce": 1404272906, "isDeleted": false, "id": "wj4e72Qj6HJ3oO41Ln6KI", "fillStyle": "solid", @@ -776,7 +776,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004139, + "updated": 1732662835798, "link": null, "locked": false, "fontSize": 20, @@ -791,8 +791,8 @@ }, { "type": "text", - "version": 327, - "versionNonce": 645262240, + "version": 328, + "versionNonce": 336096406, "isDeleted": false, "id": "h995y6B5BHW97f0bwWSO4", "fillStyle": "solid", @@ -812,7 +812,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004140, + "updated": 1732662835799, "link": null, "locked": false, "fontSize": 20, @@ -827,8 +827,8 @@ }, { "type": "text", - "version": 331, - "versionNonce": 350756448, + "version": 332, + "versionNonce": 1618137034, "isDeleted": false, "id": "aSkdLYbsJ_CUtxziuht12", "fillStyle": "solid", @@ -853,7 +853,7 @@ "type": "arrow" } ], - "updated": 1732316004140, + "updated": 1732662835799, "link": null, "locked": false, "fontSize": 20, @@ -912,8 +912,8 @@ }, { "type": "text", - "version": 417, - "versionNonce": 603420576, + "version": 418, + "versionNonce": 817523158, "isDeleted": false, "id": "dThWmBmtU3IrAAh9zarOg", "fillStyle": "solid", @@ -938,7 +938,7 @@ "type": "arrow" } ], - "updated": 1732316004140, + "updated": 1732662835799, "link": null, "locked": false, "fontSize": 20, @@ -953,8 +953,8 @@ }, { "type": "text", - "version": 344, - "versionNonce": 1393906272, + "version": 345, + "versionNonce": 500844170, "isDeleted": false, "id": "GlmIgnjxPed_iQ4GQaPwb", "fillStyle": "solid", @@ -979,7 +979,7 @@ "type": "arrow" } ], - "updated": 1732316004141, + "updated": 1732662835799, "link": null, "locked": false, "fontSize": 20, @@ -994,8 +994,8 @@ }, { "type": "text", - "version": 372, - "versionNonce": 291996576, + "version": 373, + "versionNonce": 2025504534, "isDeleted": false, "id": "0MNGPtn4EM7N2KBBVmPJ-", "fillStyle": "solid", @@ -1015,7 +1015,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004141, + "updated": 1732662835800, "link": null, "locked": false, "fontSize": 20, @@ -1030,8 +1030,8 @@ }, { "type": "text", - "version": 479, - "versionNonce": 1031538272, + "version": 480, + "versionNonce": 346350922, "isDeleted": false, "id": "lv_WKa6j3DlcVLcQTbvHK", "fillStyle": "solid", @@ -1051,7 +1051,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004141, + "updated": 1732662835800, "link": null, "locked": false, "fontSize": 20, @@ -1166,8 +1166,8 @@ }, { "type": "text", - "version": 608, - "versionNonce": 1828065184, + "version": 609, + "versionNonce": 545156182, "isDeleted": false, "id": "2vOLWhyhjuym_b7wp1ikk", "fillStyle": "solid", @@ -1187,7 +1187,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004142, + "updated": 1732662835800, "link": null, "locked": false, "fontSize": 20, @@ -1202,8 +1202,8 @@ }, { "type": "text", - "version": 847, - "versionNonce": 283916896, + "version": 848, + "versionNonce": 2116922378, "isDeleted": false, "id": "oU5YN9uWNLehtoF9m8Ue8", "fillStyle": "solid", @@ -1223,7 +1223,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004142, + "updated": 1732662835800, "link": null, "locked": false, "fontSize": 20, @@ -1238,8 +1238,8 @@ }, { "type": "text", - "version": 34, - "versionNonce": 1859328928, + "version": 35, + "versionNonce": 98202006, "isDeleted": false, "id": "u-zeGTrdFQ1ALh_rwX87a", "fillStyle": "solid", @@ -1259,7 +1259,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732316004142, + "updated": 1732662835801, "link": null, "locked": false, "fontSize": 20, @@ -1274,8 +1274,8 @@ }, { "type": "text", - "version": 1064, - "versionNonce": 816256928, + "version": 1065, + "versionNonce": 812620490, "isDeleted": false, "id": "Jj5YIVgiHR8vabD-9uHhO", "fillStyle": "solid", @@ -1295,7 +1295,7 @@ "frameId": null, "roundness": null, "boundElements": [], - "updated": 1732317187128, + "updated": 1732662835801, "link": null, "locked": false, "fontSize": 20, @@ -1309,40 +1309,112 @@ "baseline": 343 }, { - "id": "GVk6R_Wf3G2wU_hliYgA8", "type": "text", + "version": 42, + "versionNonce": 67358422, + "isDeleted": false, + "id": "GVk6R_Wf3G2wU_hliYgA8", + "fillStyle": "solid", + "strokeWidth": 2, + "strokeStyle": "solid", + "roughness": 1, + "opacity": 100, + "angle": 0, "x": 1599.486427307129, "y": 845.1953125, + "strokeColor": "#1e1e1e", + "backgroundColor": "transparent", "width": 62.4599609375, "height": 100, + "seed": 171814816, + "groupIds": [], + "frameId": null, + "roundness": null, + "boundElements": [], + "updated": 1732662835801, + "link": null, + "locked": false, + "fontSize": 20, + "fontFamily": 1, + "text": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", + "textAlign": "left", + "verticalAlign": "top", + "containerId": null, + "originalText": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", + "lineHeight": 1.25, + "baseline": 93 + }, + { + "type": "text", + "version": 63, + "versionNonce": 1770208714, + "isDeleted": false, + "id": "M90WSRig4K7lNqD6i-8d9", + "fillStyle": "solid", + "strokeWidth": 2, + "strokeStyle": "solid", + "roughness": 1, + "opacity": 100, "angle": 0, + "x": 1599.014259338379, + "y": 971.1328125, "strokeColor": "#1e1e1e", "backgroundColor": "transparent", + "width": 62.4599609375, + "height": 100, + "seed": 305063370, + "groupIds": [], + "frameId": null, + "roundness": null, + "boundElements": [], + "updated": 1732663076544, + "link": null, + "locked": false, + "fontSize": 20, + "fontFamily": 1, + "text": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", + "textAlign": "left", + "verticalAlign": "top", + "containerId": null, + "originalText": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", + "lineHeight": 1.25, + "baseline": 93 + }, + { + "type": "text", + "version": 65, + "versionNonce": 970309654, + "isDeleted": false, + "id": "G6AMmKr9lUCE0Wkzowfk5", "fillStyle": "solid", "strokeWidth": 2, "strokeStyle": "solid", "roughness": 1, "opacity": 100, + "angle": 0, + "x": 1603.932228088379, + "y": 1092.06640625, + "strokeColor": "#1e1e1e", + "backgroundColor": "transparent", + "width": 62.4599609375, + "height": 100, + "seed": 181631818, "groupIds": [], "frameId": null, "roundness": null, - "seed": 171814816, - "version": 41, - "versionNonce": 1409823648, - "isDeleted": false, - "boundElements": null, - "updated": 1732317198856, + "boundElements": [], + "updated": 1732663082245, "link": null, "locked": false, - "text": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", "fontSize": 20, "fontFamily": 1, + "text": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", "textAlign": "left", "verticalAlign": "top", - "baseline": 93, "containerId": null, "originalText": "0,2,1,3\n2,0,3,1\n2,3,1,0\n3,2,1,0", - "lineHeight": 1.25 + "lineHeight": 1.25, + "baseline": 93 } ], "appState": { diff --git a/src/r/renv.lock b/src/r/renv.lock index e3f4e50..73cd818 100644 --- a/src/r/renv.lock +++ b/src/r/renv.lock @@ -805,6 +805,18 @@ ], "Hash": "de949855009e2d4d0e52a844e30617ae" }, + "gtools": { + "Package": "gtools", + "Version": "3.9.5", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "methods", + "stats", + "utils" + ], + "Hash": "588d091c35389f1f4a9d533c8d709b35" + }, "highr": { "Package": "highr", "Version": "0.11", diff --git a/src/r/tests/testthat/test.problem-24.R b/src/r/tests/testthat/test.problem-24.R index f2175b0..d4e6b47 100644 --- a/src/r/tests/testthat/test.problem-24.R +++ b/src/r/tests/testthat/test.problem-24.R @@ -27,25 +27,50 @@ context('Testing problem 24') # output[3,] <- c(1, 2, 3, 0) # output[4,] <- c(1, 2, 0, 3) # -# output[5,] <- c(0, 2, 1, 3) -# output[6,] <- c(2, 0, 3, 1) -# output[7,] <- c(2, 3, 1, 0) -# output[8,] <- c(3, 2, 1, 0) +# output[5,] <- c(3, 1, 0, 2) +# output[6,] <- c(1, 3, 0, 2) +# output[7,] <- c(1, 0, 3, 2) +# output[8,] <- c(1, 0, 2, 3) +# +# output[9,] <- c(3, 1, 2, 0) +# output[10,] <- c(1, 3, 2, 0) +# output[11,] <- c(1, 2, 3, 0) +# output[12,] <- c(1, 2, 0, 3) +# +# output[13,] <- c(0, 2, 1, 3) +# output[14,] <- c(2, 0, 3, 1) +# output[15,] <- c(2, 3, 1, 0) +# output[16,] <- c(3, 2, 1, 0) +# +# output[17,] <- c(0, 2, 1, 3) +# output[18,] <- c(2, 0, 3, 1) +# output[19,] <- c(2, 3, 1, 0) +# output[20,] <- c(3, 2, 1, 0) +# +# output[21,] <- c(3, 0, 1, 2) +# output[22,] <- c(1, 3, 0, 2) +# output[23,] <- c(1, 2, 3, 0) +# output[24,] <- c(1, 2, 0, 3) # # expect_equal(result, output) #}) - +# test_that('Given example', { interval <- 0:2 - output <- c(12, 21, 102, 120, 201, 210) + output <- matrix(c(0, 0, 1, + 1, 2, 2, + 1, 2, 0, + 2, 0, 1, + 2, 1, 2, + 0, 1, 0), nrow = 6) - expect_equal(problem24(interval), output) + expect_equal(generateLexicographicPermutations(interval), output) }) test_that('Wanted example', { interval <- 0:9 index <- 100000 - output <- 2783915460 + output <- '2783915460' - expect_equal(problem24(interval)[index], output) + expect_equal(problem24(interval, index), output) }) \ No newline at end of file