{"id":32209961,"url":"https://github.com/jwood000/rcppalgos","last_synced_at":"2026-02-19T07:31:33.746Z","repository":{"id":26001866,"uuid":"107029937","full_name":"jwood000/RcppAlgos","owner":"jwood000","description":"Tool for Solving Problems in Combinatorics and Computational Mathematics","archived":false,"fork":false,"pushed_at":"2025-09-18T02:09:42.000Z","size":4734,"stargazers_count":45,"open_issues_count":7,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-18T04:15:16.702Z","etag":null,"topics":["combinations","combinatorics","factorization","number-theory","parallel","permutation","prime-factorizations","primesieve"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwood000.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2017-10-15T16:41:23.000Z","updated_at":"2025-02-03T14:42:38.000Z","dependencies_parsed_at":"2025-06-25T23:19:54.863Z","dependency_job_id":"97614c4e-d2f6-4c79-b2fd-9bccb4e552ae","html_url":"https://github.com/jwood000/RcppAlgos","commit_stats":{"total_commits":1278,"total_committers":3,"mean_commits":426.0,"dds":0.07276995305164324,"last_synced_commit":"f0ac079dbd016d47329b300f8ca1a1ab02f1c747"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/jwood000/RcppAlgos","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppAlgos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppAlgos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppAlgos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppAlgos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwood000","download_url":"https://codeload.github.com/jwood000/RcppAlgos/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppAlgos/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280391148,"owners_count":26322868,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-22T02:00:06.515Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["combinations","combinatorics","factorization","number-theory","parallel","permutation","prime-factorizations","primesieve"],"created_at":"2025-10-22T06:21:54.846Z","updated_at":"2025-10-22T06:21:58.983Z","avatar_url":"https://github.com/jwood000.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# RcppAlgos \u003cimg src='man/figures/RcppAlgos-logo.png' width=\"150px\" align=\"right\" /\u003e\n\n\u003c!-- badges: start --\u003e\n[![CRAN status](\u003chttps://www.r-pkg.org/badges/version/RcppAlgos\u003e)](\u003chttps://cran.r-project.org/package=RcppAlgos\u003e)\n![](\u003chttps://cranlogs.r-pkg.org/badges/RcppAlgos?color=orange\u003e)\n![](\u003chttps://cranlogs.r-pkg.org/badges/grand-total/RcppAlgos?color=brightgreen\u003e)\n[![Codacy Badge](\u003chttps://app.codacy.com/project/badge/Grade/e7fef773f6514aa4a2decda9adf57ae8\u003e)](\u003chttps://app.codacy.com/gh/jwood000/RcppAlgos/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade\u003e)\n[![Dependencies](\u003chttps://tinyverse.netlify.app/badge/RcppAlgos\u003e)](\u003chttps://cran.r-project.org/package=RcppAlgos\u003e)\n[![R-CMD-check](\u003chttps://github.com/jwood000/RcppAlgos/actions/workflows/R-CMD-check.yml/badge.svg\u003e)](\u003chttps://github.com/jwood000/RcppAlgos/actions/workflows/R-CMD-check.yml\u003e)\n[![Codecov test coverage](\u003chttps://codecov.io/gh/jwood000/RcppAlgos/branch/main/graph/badge.svg\u003e)](\u003chttps://app.codecov.io/gh/jwood000/RcppAlgos?branch=main\u003e)\n\u003c!-- badges: end --\u003e\n\nA collection of high performance functions and iterators implemented in C++ for solving problems in combinatorics and computational mathematics.\n\n## Featured Functions\n\n  - **`{combo|permute}General`**: Generate all combinations/permutations of a vector (including [multisets](\u003chttps://en.wikipedia.org/wiki/Multiset\u003e)) meeting specific criteria.\n  - **`{partitions|compositions}General`**: Efficient algorithms for partitioning numbers under various constraints\n  - **`{expandGrid|comboGrid}`**: Generate traditional Cartesian product as well as the product where order does not matter.\n  - **`{combo|permute|partitions|compositions|expandGrid|comboGroups}Sample`**: Generate reproducible random samples\n  - **`{combo|permute|partitions|compositions|expandGrid|comboGroups}Iter`**: Flexible iterators allow for bidirectional iteration as well as random access.\n  - **`primeSieve`**: Fast prime number generator\n  - **`primeCount`**: Prime counting function using [Legendre's formula](\u003chttp://mathworld.wolfram.com/LegendresFormula.html\u003e)\n\nThe `primeSieve` function and the `primeCount` function are both based off of the excellent work by [Kim Walisch](\u003chttps://github.com/kimwalisch\u003e). The respective repos can be found here: [kimwalisch/primesieve](\u003chttps://github.com/kimwalisch/primesieve\u003e); [kimwalisch/primecount](\u003chttps://github.com/kimwalisch/primecount\u003e)\n\nAdditionally, many of the sieving functions make use of the fast integer division library [libdivide](\u003chttps://github.com/ridiculousfish/libdivide\u003e) by [ridiculousfish](\u003chttps://github.com/ridiculousfish\u003e).\n\n## Benchmarks\n\n* [High Performance Benchmarks](\u003chttps://jwood000.github.io/RcppAlgos/articles/HighPerformanceBenchmarks.html\u003e)\n\n## Installation\n\n``` r\ninstall.packages(\"RcppAlgos\")\n\n## install the development version\ndevtools::install_github(\"jwood000/RcppAlgos\")\n```\n\n## Usage\n\n### Combinatorics Basics\n\n``` r\n## Find all 3-tuples combinations of 1:4\ncomboGeneral(4, 3)\n#\u003e      [,1] [,2] [,3]\n#\u003e [1,]   1    2    3\n#\u003e [2,]   1    2    4\n#\u003e [3,]   1    3    4\n#\u003e [4,]   2    3    4\n\n\n## Alternatively, iterate over combinations\na = comboIter(4, 3)\na@nextIter()\n#\u003e [1] 1 2 3\n\na@back()\n#\u003e [1] 2 3 4\n\na[[2]]\n#\u003e [1] 1 2 4\n\n\n## Pass any atomic type vector\npermuteGeneral(letters, 3, upper = 4)\n#\u003e      [,1] [,2] [,3]\n#\u003e [1,] \"a\"  \"b\"  \"c\"\n#\u003e [2,] \"a\"  \"b\"  \"d\"\n#\u003e [3,] \"a\"  \"b\"  \"e\"\n#\u003e [4,] \"a\"  \"b\"  \"f\"\n\n\n## Generate a reproducible sample\ncomboSample(10, 8, TRUE, n = 5, seed = 84)\n#\u003e      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]\n#\u003e [1,]    3    3    3    6    6   10   10   10\n#\u003e [2,]    1    3    3    4    4    7    9   10\n#\u003e [3,]    3    7    7    7    9   10   10   10\n#\u003e [4,]    3    3    3    9   10   10   10   10\n#\u003e [5,]    1    2    2    3    3    4    4    7\n```\n\n### Integer Partitions and Constraints\n\n``` r\n## Flexible partitioning algorithms\npartitionsGeneral(0:5, 3, freqs = rep(1:2, 3), target = 6)\n#\u003e      [,1] [,2] [,3]\n#\u003e [1,]    0    1    5\n#\u003e [2,]    0    2    4\n#\u003e [3,]    0    3    3\n#\u003e [4,]    1    1    4\n#\u003e [5,]    1    2    3\n\n\n## And compositions\ncompositionsGeneral(0:3, repetition = TRUE)\n#\u003e      [,1] [,2] [,3]\n#\u003e [1,]    0    0    3\n#\u003e [2,]    0    1    2\n#\u003e [3,]    0    2    1\n#\u003e [4,]    1    1    1\n\n\n## Get combinations such that the product is between\n## 3600 and 4000 (including 3600 but not 4000)\ncomboGeneral(5, 7, TRUE, constraintFun = \"prod\",\n             comparisonFun = c(\"\u003e=\",\"\u003c\"),\n             limitConstraints = c(3600, 4000),\n             keepResults = TRUE)\n#\u003e      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]\n#\u003e [1,]    1    2    3    5    5    5    5 3750\n#\u003e [2,]    1    3    3    4    4    5    5 3600\n#\u003e [3,]    1    3    4    4    4    4    5 3840\n#\u003e [4,]    2    2    3    3    4    5    5 3600\n#\u003e [5,]    2    2    3    4    4    4    5 3840\n#\u003e [6,]    3    3    3    3    3    3    5 3645\n#\u003e [7,]    3    3    3    3    3    4    4 3888\n\n\n## We can even iterate over constrained cases. These are\n## great when we don't know how many results there are upfront.\n## Save on memory and still at the speed of C++!!\np = permuteIter(5, 7, TRUE, constraintFun = \"prod\",\n                comparisonFun = c(\"\u003e=\",\"\u003c\"),\n                limitConstraints = c(3600, 4000),\n                keepResults = TRUE)\n\n## Get the next n results\nt = p@nextNIter(1048)\n\n## N.B. keepResults = TRUE adds the 8th column\ntail(t)\n#\u003e         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]\n#\u003e [1043,]    5    4    4    3    4    1    4 3840\n#\u003e [1044,]    5    4    4    3    4    4    1 3840\n#\u003e [1045,]    5    4    4    4    1    3    4 3840\n#\u003e [1046,]    5    4    4    4    1    4    3 3840\n#\u003e [1047,]    5    4    4    4    3    1    4 3840\n#\u003e [1048,]    5    4    4    4    3    4    1 3840\n\n## Continue iterating from where we left off\np@nextIter()\n#\u003e [1]    5    4    4    4    4    1    3 3840\n\np@nextIter()\n#\u003e [1]    5    4    4    4    4    3    1 3840\n\np@nextIter()\n#\u003e [1]    2    2    3    3    4    5    5 3600\n\n## N.B. totalResults and totalRemaining are NA because there is no\n## closed form solution for determining this.\np@summary()\n#\u003e $description\n#\u003e [1] \"Permutations with repetition of 5 choose 7 where the prod is between 3600 and 4000\"\n#\u003e \n#\u003e $currentIndex\n#\u003e [1] 1051\n#\u003e \n#\u003e $totalResults\n#\u003e [1] NA\n#\u003e \n#\u003e $totalRemaining\n#\u003e [1] NA\n```\n\n### Cartesian Products\n\n``` r\n## Base R expand.grid returns a data.frame by default\n## and varies the first column the fastest\nbR = expand.grid(rep(list(1:3), 3))\nhead(bR, n = 3)\n#\u003e   Var1 Var2 Var3\n#\u003e 1    1    1    1\n#\u003e 2    2    1    1\n#\u003e 3    3    1    1\n\ntail(bR, n = 3)\n#\u003e    Var1 Var2 Var3\n#\u003e 25    1    3    3\n#\u003e 26    2    3    3\n#\u003e 27    3    3    3\n\n\n## RcppAlgos::expandGrid returns a matrix if the input is of\n## the same class, otherwise it returns a data.frame. Also\n## varies the first column the slowest.\nalgos = expandGrid(rep(list(1:3), 3))\nhead(algos, n = 3)\n#\u003e      Var1 Var2 Var3\n#\u003e [1,]    1    1    1\n#\u003e [2,]    1    1    2\n#\u003e [3,]    1    1    3\n\ntail(algos, n = 3)\n#\u003e       Var1 Var2 Var3\n#\u003e [25,]    3    3    1\n#\u003e [26,]    3    3    2\n#\u003e [27,]    3    3    3\n\n\n## N.B. Since we are passing more than one type, a data.frame is returned\nexpandGrid(\n    c(rep(list(letters[1:3]), 3), list(1:3)),\n    upper = 3\n)\n#\u003e   Var1 Var2 Var3 Var4\n#\u003e 1    a    a    a    1\n#\u003e 2    a    a    a    2\n#\u003e 3    a    a    a    3\n\n\n## With RcppAlgos::comboGrid order doesn't matter, so c(1, 1, 2),\n## c(1, 2, 1), and c(2, 1, 1) are the same.\ncomboGrid(rep(list(1:3), 3))\n#\u003e       Var1 Var2 Var3\n#\u003e  [1,]    1    1    1\n#\u003e  [2,]    1    1    2\n#\u003e  [3,]    1    1    3\n#\u003e  [4,]    1    2    2\n#\u003e  [5,]    1    2    3\n#\u003e  [6,]    1    3    3\n#\u003e  [7,]    2    2    2\n#\u003e  [8,]    2    2    3\n#\u003e  [9,]    2    3    3\n#\u003e [10,]    3    3    3\n\n\n## If you don't want any repeats, set repetition = FALSE\ncomboGrid(rep(list(1:3), 3), repetition = FALSE)\n#\u003e      Var1 Var2 Var3\n#\u003e [1,]    1    2    3\n```\n\n### Partitions of Groups\n\nEfficiently partition a vector into groups with `comboGroups`. For example, the code below finds all possible pairings of groups of size 3 vs groups of size 2 (See this stackoverflow post: [Find all possible team pairing schemes](\u003chttps://stackoverflow.com/a/76068476/4408538\u003e)). \n``` r\nplayers \u003c- c(\"Ross\", \"Bobby\", \"Max\", \"Casper\", \"Jake\")\ncomboGroups(players, grpSizes = c(2, 3))\n#\u003e       Grp1     Grp1     Grp2    Grp2     Grp2    \n#\u003e  [1,] \"Ross\"   \"Bobby\"  \"Max\"   \"Casper\" \"Jake\"  \n#\u003e  [2,] \"Ross\"   \"Max\"    \"Bobby\" \"Casper\" \"Jake\"  \n#\u003e  [3,] \"Ross\"   \"Casper\" \"Bobby\" \"Max\"    \"Jake\"  \n#\u003e  [4,] \"Ross\"   \"Jake\"   \"Bobby\" \"Max\"    \"Casper\"\n#\u003e  [5,] \"Bobby\"  \"Max\"    \"Ross\"  \"Casper\" \"Jake\"  \n#\u003e  [6,] \"Bobby\"  \"Casper\" \"Ross\"  \"Max\"    \"Jake\"  \n#\u003e  [7,] \"Bobby\"  \"Jake\"   \"Ross\"  \"Max\"    \"Casper\"\n#\u003e  [8,] \"Max\"    \"Casper\" \"Ross\"  \"Bobby\"  \"Jake\"  \n#\u003e  [9,] \"Max\"    \"Jake\"   \"Ross\"  \"Bobby\"  \"Casper\"\n#\u003e [10,] \"Casper\" \"Jake\"   \"Ross\"  \"Bobby\"  \"Max\"\n```\n\n### Computational Mathematics\n\n``` r\n## Generate prime numbers\nprimeSieve(50)\n#\u003e [1]  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47\n\n## Many of the functions can produce results in\n## parallel for even greater performance\np = primeSieve(1e15, 1e15 + 1e8, nThreads = 4)\n\nhead(p)\n#\u003e [1] 1000000000000037 1000000000000091 1000000000000159\n#\u003e [4] 1000000000000187 1000000000000223 1000000000000241\ntail(p)\n#\u003e [1] 1000000099999847 1000000099999867 1000000099999907\n#\u003e [4] 1000000099999919 1000000099999931 1000000099999963\n\n\n## Count prime numbers less than n\nprimeCount(1e10)\n#\u003e [1] 455052511\n\n## Get the prime factorization\nset.seed(24028)\nprimeFactorize(sample(1e15, 3), namedList = TRUE)\n#\u003e $`701030825091514`\n#\u003e [1]             2           149 2352452433193\n#\u003e \n#\u003e $`83054168594779`\n#\u003e [1]  3098071 26808349\n#\u003e \n#\u003e $`397803024735610`\n#\u003e [1]            2            5           13           13 235386405169\n```\n\n## Further Reading\n\n* [Function Documentation](\u003chttps://jwood000.github.io/RcppAlgos/reference/index.html\u003e)\n* [Computational Mathematics Overview](\u003chttps://jwood000.github.io/RcppAlgos/articles/ComputationalMathematics.html\u003e)\n* [Combination and Permutation Basics](\u003chttps://jwood000.github.io/RcppAlgos/articles/GeneralCombinatorics.html\u003e)\n* [Combinatorial Sampling](\u003chttps://jwood000.github.io/RcppAlgos/articles/CombinatorialSampling.html\u003e)\n* [Constraints, Partitions, and Compositions](\u003chttps://jwood000.github.io/RcppAlgos/articles/CombPermConstraints.html\u003e)\n* [Attacking Problems Related to the Subset Sum Problem](\u003chttps://jwood000.github.io/RcppAlgos/articles/SubsetSum.html\u003e)\n* [Combinatorial Iterators in RcppAlgos](\u003chttps://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html\u003e)\n* [Cartesian Products and Partitions of Groups](\u003chttps://jwood000.github.io/RcppAlgos/articles/OtherCombinatorics.html\u003e)\n\n## Why **`RcppAlgos`** but no **`Rcpp`**?\n\nPrevious versions of `RcppAlgos` relied on [Rcpp](\u003chttps://github.com/RcppCore/Rcpp\u003e) to ease the burden of exposing C++ to R. While the current version of `RcppAlgos` does not utilize `Rcpp`, it would not be possible without the myriad of excellent contributions to `Rcpp`.\n\n## Contact\n\nIf you would like to report a bug, have a question, or have suggestions for possible improvements, please file an [issue](\u003chttps://github.com/jwood000/RcppAlgos/issues\u003e).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwood000%2Frcppalgos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwood000%2Frcppalgos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwood000%2Frcppalgos/lists"}