{"id":32207751,"url":"https://github.com/jwood000/rcppbigintalgos","last_synced_at":"2025-10-22T05:59:09.341Z","repository":{"id":44453880,"uuid":"107071383","full_name":"jwood000/RcppBigIntAlgos","owner":"jwood000","description":"R Package for Factoring Big Integers using the C Library GMP (GNU Multiple Precision Arithmetic)","archived":false,"fork":false,"pushed_at":"2024-06-04T02:44:34.000Z","size":631,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-22T05:59:06.871Z","etag":null,"topics":["algorithm","gmp","integer-factorization","mpqs","prime-factorizations","primes","quadratic-sieve","quadratic-sieve-algorithm"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwood000.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS","contributing":null,"funding":null,"license":null,"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-16T02:58:58.000Z","updated_at":"2024-08-16T03:54:25.000Z","dependencies_parsed_at":"2024-06-04T03:51:37.707Z","dependency_job_id":null,"html_url":"https://github.com/jwood000/RcppBigIntAlgos","commit_stats":{"total_commits":377,"total_committers":2,"mean_commits":188.5,"dds":"0.19098143236074272","last_synced_commit":"f252d5436c87055bda8a985ef38b87d035ea1e3d"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/jwood000/RcppBigIntAlgos","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppBigIntAlgos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppBigIntAlgos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppBigIntAlgos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppBigIntAlgos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwood000","download_url":"https://codeload.github.com/jwood000/RcppBigIntAlgos/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwood000%2FRcppBigIntAlgos/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280389299,"owners_count":26322507,"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":["algorithm","gmp","integer-factorization","mpqs","prime-factorizations","primes","quadratic-sieve","quadratic-sieve-algorithm"],"created_at":"2025-10-22T05:59:05.089Z","updated_at":"2025-10-22T05:59:09.328Z","avatar_url":"https://github.com/jwood000.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![CRAN status](\u003chttps://www.r-pkg.org/badges/version/RcppBigIntAlgos\u003e)](\u003chttps://cran.r-project.org/package=RcppBigIntAlgos\u003e)\n[![R build status](\u003chttps://github.com/jwood000/RcppBigIntAlgos/workflows/R-CMD-check/badge.svg\u003e)](\u003chttps://github.com/jwood000/RcppBigIntAlgos/actions\u003e)\n![](\u003chttp://cranlogs.r-pkg.org/badges/RcppBigIntAlgos?color=orange\u003e)\n![](\u003chttp://cranlogs.r-pkg.org/badges/grand-total/RcppBigIntAlgos?color=brightgreen\u003e)\n[![Dependencies](\u003chttps://tinyverse.netlify.app/badge/RcppBigIntAlgos\u003e)](\u003chttps://cran.r-project.org/package=RcppBigIntAlgos\u003e)\n[![Coverage status](\u003chttps://codecov.io/github/jwood000/RcppBigIntAlgos/branch/main/graph/badge.svg?token=LMO4DH4OtN\u003e)](\u003chttps://app.codecov.io/github/jwood000/RcppBigIntAlgos\u003e)\n[![Codacy Badge](\u003chttps://app.codacy.com/project/badge/Grade/3be4c3c9e3554125b8cc0e13decaf95c\u003e)](\u003chttps://app.codacy.com/gh/jwood000/RcppBigIntAlgos/dashboard?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=jwood000/RcppBigIntAlgos\u0026utm_campaign=Badge_Grade\u003e)\n\n# RcppBigIntAlgos\n\n## Overview\n\n`RcppBigIntAlgos` is an `R` package for efficiently factoring large integers. It is multithreaded and uses the C library GMP (GNU Multiple Precision Arithmetic). For very large integers, prime factorization is carried out by a variant of the quadratic sieve algorithm that implements multiple polynomials. For smaller integers, a simple elliptic curve algorithm is attempted followed by a constrained version of the Pollard’s rho algorithm (original code from \u003chttps://gmplib.org/\u003e… this is the same algorithm found in the [R gmp package](\u003chttps://CRAN.R-project.org/package=gmp\u003e) called by the function `factorize`). Finally, one can quickly obtain a complete factorization of a given number `n` via `divisorsBig`.\n\n## Installation\n\n``` r\n## install.packages(\"RcppBigIntAlgos\")\n\n## Or install the development version\n## devtools::install_github(\"jwood000/RcppBigIntAlgos\")\n```\n\n## The Quadratic Sieve\n\nThe function `quadraticSieve` implements the multiple polynomial quadratic sieve algorithm. Currently, `quadraticSieve` can comfortably factor numbers with less than 70 digits (~230 bits) on most standard personal computers. If you have access to powerful computers with many cores, factoring 100+ digit semiprimes in less than a day is not out of the question. Note, the function `primeFactorizeBig(n, skipECM = T, skipPolRho = T)` is the same as `quadraticSieve(n)`.\n\n``` r\nlibrary(gmp)\n#\u003e \n#\u003e Attaching package: 'gmp'\n#\u003e The following objects are masked from 'package:base':\n#\u003e \n#\u003e     %*%, apply, crossprod, matrix, tcrossprod\nlibrary(RcppBigIntAlgos)\n\n## Generate large semi-primes\nsemiPrime120bits \u003c- prod(nextprime(urand.bigz(2, 60, 42)))\n#\u003e Seed default initialisation\n#\u003e Seed initialisation\nsemiPrime130bits \u003c- prod(nextprime(urand.bigz(2, 65, 1)))\n#\u003e Seed initialisation\nsemiPrime140bits \u003c- prod(nextprime(urand.bigz(2, 70, 42)))\n#\u003e Seed initialisation\n\n## The 120 bit number is 36 digits\nnchar(as.character(semiPrime120bits))\n#\u003e [1] 36\n\n## The 130 bit number is 39 digits\nnchar(as.character(semiPrime130bits))\n#\u003e [1] 39\n\n## The 140 bit number is 42 digits\nnchar(as.character(semiPrime140bits))\n#\u003e [1] 42\n\n## Using factorize from gmp package which implements pollard's rho algorithm\n##**************gmp::factorize*********************\nsystem.time(print(factorize(semiPrime120bits)))\n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 638300143449131711  1021796573707617139\n#\u003e    user  system elapsed \n#\u003e 100.788   0.291 101.112 \n\nsystem.time(print(factorize(semiPrime130bits)))\n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 14334377958732970351 29368224335577838231\n#\u003e      user   system  elapsed \n#\u003e  1253.060    2.612 1256.065 \n \nsystem.time(print(factorize(semiPrime140bits)))\n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 143600566714698156857  1131320166687668315849\n#\u003e     user   system  elapsed \n#\u003e 1673.666    3.154 1676.838 \n\n\n##**************quadraticSieve*********************\n## quadraticSieve is much faster and scales better\nsystem.time(print(quadraticSieve(semiPrime120bits)))\n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 638300143449131711  1021796573707617139\n#\u003e    user  system elapsed \n#\u003e   0.041   0.000   0.042\n\nsystem.time(print(quadraticSieve(semiPrime130bits)))\n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 14334377958732970351 29368224335577838231\n#\u003e    user  system elapsed \n#\u003e   0.051   0.001   0.052\n\nsystem.time(print(quadraticSieve(semiPrime140bits)))\n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 143600566714698156857  1131320166687668315849\n#\u003e    user  system elapsed \n#\u003e   0.077   0.001   0.078\n```\n### Using Multiple Threads\n\nAs of version `0.3.0`, we can utilize multiple threads. Below, are a few examples:\n\n1.  The largest [Cunnaningham Most Wanted](\u003chttps://www.lehigh.edu/~bad0/msg06332.html\u003e) number from the first edition released in 1983 in less than 14 seconds.\n2.  [RSA-79](\u003chttps://members.loria.fr/PZimmermann/records/rsa.html\u003e) under 2 minutes.\n3.  A 300-bit (91-digits) semiprime in 1 hour.\n4.  [RSA-99](\u003chttps://members.loria.fr/PZimmermann/records/rsa.html\u003e) under 5 hours.\n5.  [RSA-100](\u003chttps://en.wikipedia.org/wiki/RSA-100\u003e) under 10 hours.\n\nBelow are my machine specs and R version info:\n\n``` r\n## MacBook Air (2022)\n## Processor: Apple M2\n## Memory; 24 GB\n\nsessionInfo()\n#\u003e R version 4.3.1 (2023-06-16)\n#\u003e Platform: aarch64-apple-darwin20 (64-bit)\n#\u003e Running under: macOS Ventura 13.4.1\n#\u003e \n#\u003e Matrix products: default\n#\u003e BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib \n#\u003e LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0\n#\u003e \n#\u003e locale:\n#\u003e [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8\n#\u003e \n#\u003e time zone: America/New_York\n#\u003e tzcode source: internal\n#\u003e \n#\u003e attached base packages:\n#\u003e [1] stats     graphics  grDevices utils     datasets  methods   base     \n#\u003e \n#\u003e other attached packages:\n#\u003e [1] RcppBigIntAlgos_1.1.0    gmp_0.7-2\n#\u003e \n#\u003e loaded via a namespace (and not attached):\n#\u003e [1] compiler_4.3.1\n\n## Maximum number of available threads\nstdThreadMax()\n#\u003e [1] 8\n```\n\n#### Most Wanted 1983\n\n```r\nmostWanted1983 \u003c- as.bigz(div.bigz(sub.bigz(pow.bigz(10, 71), 1), 9))\nquadraticSieve(mostWanted1983, showStats = TRUE, nThreads = 8)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     11111111111111111111111111111111111111111111111111111111111111111111111\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |      10s 940ms     |   100%   |    16183    |    4089    |    4345    |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |      2s 107ms      |     8301 x 8434    |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |      13s 173ms     |\n#\u003e \n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 241573142393627673576957439049           \n#\u003e [2] 45994811347886846310221728895223034301839\n```\n\n#### RSA-79\n\n```r\nrsa79 \u003c- as.bigz(\"7293469445285646172092483905177589838606665884410340391954917800303813280275279\")\nquadraticSieve(rsa79, showStats = TRUE, nThreads = 8)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     7293469445285646172092483905177589838606665884410340391954917800303813280275279\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |     1m 35s 89ms    |   100%   |    91221    |    5651    |    7096    |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |      5s 175ms      |    12625 x 12747   |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |    1m 40s 558ms    |\n#\u003e \n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 848184382919488993608481009313734808977 \n#\u003e [2] 8598919753958678882400042972133646037727\n```\n\n### Random 300-bit Semiprime\n\n```r\nsemi_prime_300_bit \u003c- prod(nextprime(urand.bigz(nb = 2, size = 150, seed = 42)))\n#\u003e Seed default initialisation\n#\u003e Seed initialisation\n\nnchar(as.character(semi_prime_300_bit))\n#\u003e [1] 91\n\nquadraticSieve(semi_prime_300_bit, showStats=TRUE, nThreads=8)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     1598678911004402782180963020655649301157676037614983547537229086778878619660017752047003277\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |   1h 3m 4s 604ms   |   100%   |   2447868   |    6835    |    12143   |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |      13s 202ms     |    18892 x 18978   |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |   1h 3m 18s 733ms  |\n#\u003e \n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 1262920060924323380524693068285713630353017593\n#\u003e [2] 1265859146963220756974764147023611562195937589\n```\n\n#### RSA-99\n\n```r\nrsa99 \u003c- \"256724393281137036243618548169692747168133997830674574560564321074494892576105743931776484232708881\"\n \nquadraticSieve(rsa99, showStats=TRUE, nThreads=8)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     256724393281137036243618548169692747168133997830674574560564321074494892576105743931776484232708881\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |  4h 41m 28s 410ms  |   100%   |   7351308   |    9203    |    15846   |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |      31s 359ms     |    24929 x 25049   |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |   4h 42m 1s 55ms   |\n#\u003e \n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 4868376167980921239824329271069101142472222111193 \n#\u003e [2] 52733064254484107837300974402288603361507691060217\n```\n\n#### RSA-100\n\n```r\nrsa100 \u003c- \"1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139\"\n\nquadraticSieve(rsa100, showStats = TRUE, nThreads = 8)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |   9h 43m 9s 13ms   |   100%   |   14999534  |    9230    |    16665   |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |      34s 414ms     |    25799 x 25895   |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |  9h 43m 45s 242ms  |\n#\u003e \n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 37975227936943673922808872755445627854565536638199\n#\u003e [2] 40094690950920881030683735292761468389214899724061\n```\n\n## **`primeFactorizeBig`**\n\nAs of version `1.0.0`, we can take advantage of the power of Lenstra’s elliptic curve method. This method is particularly useful for quickly finding smaller prime factors of very large composite numbers. It is automatically utilized in the vectorized prime factorization function `primeFactorizeBig`. This function should be preferred in most situations and is identical to `quadraticSieve` when both `skipECM` and `skipPolRho` are set to `TRUE`.\n\nIt is optimized for factoring big and small numbers by dynamically using different algorithms based off of the input. It takes cares to not spend too much time in any of the methods and avoids wastefully switching to the quadratic sieve when the number is very large.\n\nFor example, using the defaults on `mostWanted1983` above only adds a few seconds.\n\n```r\nprimeFactorizeBig(mostWanted1983, showStats = TRUE, nThreads = 8)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     11111111111111111111111111111111111111111111111111111111111111111111111\n#\u003e \n#\u003e |  Pollard Rho Time  |\n#\u003e |--------------------|\n#\u003e |        90ms        |\n#\u003e \n#\u003e |  Lenstra ECM Time  |  Number of Curves  |\n#\u003e |--------------------|--------------------|\n#\u003e |      2s 129ms      |        4181        |\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |      10s 637ms     |   100%   |    16183    |    4089    |    4345    |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |       2s 73ms      |     8301 x 8434    |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |       15s 7ms      |\n#\u003e \n#\u003e Big Integer ('bigz') object of length 2:\n#\u003e [1] 241573142393627673576957439049           \n#\u003e [2] 45994811347886846310221728895223034301839\n```\n\n\n### The Lentra’s power\n\nPerforming a few iterations of the Pollard’s rho algorithm followed by the quadratic sieve can prove to be a terrible solution when the input size is incredibly large. More than likely, your constrained Pollard’s rho algorithm won’t find any prime factors, and eventually will pass an enormous composite number to the quadratic sieve. The quadratic sieve isn’t optimized for finding *smallish* factors in large composites. It will treat the input similarly to a semiprime of the same size. For example, the number `ecmExpo1` below is 428 digits and if passed to the quadratic sieve algorithm, it could take years to factor as not even the most sophisticated semiprime algorithms can easily factor numbers of this size (See \u003chttps://en.wikipedia.org/wiki/RSA_Factoring_Challenge\u003e). However, with the ECM, this is light work.\n\n``` r\necmExpo1 \u003c- pow.bigz(prod(nextprime(urand.bigz(10, 47, 13))), 3) *\n            nextprime(urand.bigz(1, 47, 123))\n#\u003e Seed initialisation\n#\u003e Seed initialisation\n\nsystem.time(primeFactorizeBig(ecmExpo1, skipPolRho = TRUE, nThreads = 8))\n#\u003e    user  system elapsed \n#\u003e  10.768   0.024   2.840\n\necmExpo2 \u003c- pow.bigz(prod(nextprime(urand.bigz(10, 40, 42))), 3) *\n            pow.bigz(prod(nextprime(urand.bigz(10, 45, 42))), 5) *\n            nextprime(urand.bigz(1, 80, 123)) *\n            nextprime(urand.bigz(1, 90, 123))\n#\u003e Seed initialisation\n#\u003e Seed initialisation\n#\u003e Seed initialisation\n#\u003e Seed initialisation\n\necm2 \u003c- primeFactorizeBig(ecmExpo2, nThreads = 8, showStats = TRUE)\n#\u003e \n#\u003e Summary Statistics for Factoring:\n#\u003e     215590243996472403826986190959172968924582673399860040722565967228161406366421528160220759715319487748182459438846599849086479492406371823155743767776724831907786824847947911279318978691074995051134928403920242678621702805852530836549215396392496547012632510254095452820560897056877208839289455859596425985161415486708317667524364034985007498553043810662487802224636724842739870398138687583364019516727138808818929150447675931521160760469848585726320428719422689614460288917155690252498420348768667307236940996727406088066077174182630522224530495122361513443818343671556622023320404680434511134662288782134897895034832405624062328931996299632052035105693354967480119521413889950571647670948619957219258020882926218179673783234886719528724327466781424377562596208305773866403070803284721517038640418360492984835882655310830736014378303508492181368455405610072971016964625940147583833553074577246736323251622391837090678894432642161330574148459946482734705537584444739753686240736188950594612747228733363204312823260997792251300802876878725604381077823\n#\u003e \n#\u003e |  Pollard Rho Time  |\n#\u003e |--------------------|\n#\u003e |      13s 163ms     |\n#\u003e \n#\u003e |  Lenstra ECM Time  |  Number of Curves  |\n#\u003e |--------------------|--------------------|\n#\u003e |      15s 256ms     |        24565       |\n#\u003e \n#\u003e |      MPQS Time     | Complete | Polynomials |   Smooths  |  Partials  |\n#\u003e |--------------------|----------|-------------|------------|------------|\n#\u003e |        419ms       |   100%   |     1482    |    1013    |    1450    |\n#\u003e \n#\u003e |  Mat Algebra Time  |    Mat Dimension   |\n#\u003e |--------------------|--------------------|\n#\u003e |        60ms        |     2425 x 2463    |\n#\u003e \n#\u003e |     Total Time     |\n#\u003e |--------------------|\n#\u003e |      28s 648ms     |\n\nhead(ecm2)\n#\u003e Big Integer ('bigz') object of length 6:\n#\u003e [1] 66642484459  66642484459  66642484459  385217678221 385217678221\n#\u003e [6] 385217678221\n\ntail(ecm2)\n#\u003e Big Integer ('bigz') object of length 6:\n#\u003e [1] 34936543827863               34936543827863              \n#\u003e [3] 34936543827863               34936543827863              \n#\u003e [5] 1130548241045557299883517    1051687085486158310119550449\n\nlength(ecm2)\n#\u003e [1] 82\n```\n\n### Safely Interrupt Execution\n\nIf you want to interrupt a command which will take a long time, hit Ctrl + c, or esc if using RStudio, to stop execution. When you utilize multiple threads with a very large number (e.g. 90 digit semiprime), you will be able to interrupt execution once every ~30 seconds.\n\n``` r\n## User hits Ctrl + c\n## system.time(quadraticSieve(prod(nextprime(urand.bigz(2, 100, 42)))))\n## Seed initialisation\n## Error in PrimeFactorization(n, FALSE, showStats, TRUE, TRUE, nThreads,  :\n##   C++ call interrupted by the user.\n## Timing stopped at: 2.164 0.039 2.167\n```\n\n## Complete Factorization with **`divisorsBig`**\n\nThis function generates the complete factorization for many (possibly large) numbers. It is vectorized and can also return a named list.\n\n``` r\n## Get all divisors of a given number:\ndivisorsBig(1000)\n#\u003e Big Integer ('bigz') object of length 16:\n#\u003e  [1] 1    2    4    5    8    10   20   25   40   50   100  125  200  250  500 \n#\u003e [16] 1000\n\n## Or, get all divisors of a vector:\ndivisorsBig(urand.bigz(nb = 2, size = 100, seed = 42), namedList = TRUE)\n#\u003e Seed initialisation\n#\u003e $`153675943236425922379228498617`\n#\u003e Big Integer ('bigz') object of length 16:\n#\u003e  [1] 1                              3                             \n#\u003e  [3] 7                              9                             \n#\u003e  [5] 21                             27                            \n#\u003e  [7] 63                             189                           \n#\u003e  [9] 813100228764158319466817453    2439300686292474958400452359  \n#\u003e [11] 5691701601349108236267722171   7317902058877424875201357077  \n#\u003e [13] 17075104804047324708803166513  21953706176632274625604071231 \n#\u003e [15] 51225314412141974126409499539  153675943236425922379228498617\n#\u003e \n#\u003e $`261352009818227569107309994396`\n#\u003e Big Integer ('bigz') object of length 12:\n#\u003e  [1] 1                              2                             \n#\u003e  [3] 4                              155861                        \n#\u003e  [5] 311722                         623444                        \n#\u003e  [7] 419206873140534785974859       838413746281069571949718      \n#\u003e  [9] 1676827492562139143899436      65338002454556892276827498599 \n#\u003e [11] 130676004909113784553654997198 261352009818227569107309994396\n```\n\n### Efficiency\n\nIt is very efficient as well. It is equipped with a modified merge sort algorithm that significantly outperforms the `std::sort`/`bigvec` (the class utilized in the `R gmp` package) combination.\n\n``` r\nhugeNumber \u003c- pow.bigz(2, 100) * pow.bigz(3, 100) * pow.bigz(5, 100)\nsystem.time(overOneMillion \u003c- divisorsBig(hugeNumber))\n#\u003e    user  system elapsed \n#\u003e   0.117   0.008   0.126\n\nlength(overOneMillion)\n#\u003e [1] 1030301\n\n## Output is in ascending order\ntail(overOneMillion)\n#\u003e Big Integer ('bigz') object of length 6:\n#\u003e [1] 858962534553352218394101882942702121170179203335000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 \n#\u003e [2] 1030755041464022662072922259531242545404215044002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n#\u003e [3] 1288443801830028327591152824414053181755268805002500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n#\u003e [4] 1717925069106704436788203765885404242340358406670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n#\u003e [5] 2576887603660056655182305648828106363510537610005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n#\u003e [6] 5153775207320113310364611297656212727021075220010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n```\n\n### Correct Ordering\n\nAnother benefit is that it will return correct orderings on extremely large numbers when compared to sorting large vectors in `base R`. Typically in `base R` you must execute the following: `order(asNumeric(myVectorHere))`. When the numbers get large enough, precision is lost which leads to incorrect orderings. Observe:\n\n``` r\nset.seed(101)\ntestBaseSort \u003c- do.call(\n    c, lapply(sample(100), \\(x) add.bigz(pow.bigz(10,80), x))\n)\ntestBaseSort \u003c- testBaseSort[order(asNumeric(testBaseSort))]\nmyDiff \u003c- do.call(\n    c, lapply(1:99, \\(x) sub.bigz(testBaseSort[x+1], testBaseSort[x]))\n)\n\n## Should return integer(0) as the difference should always be positive\n## NOTE that the result will be unpredictable because of lack of precision\nwhich(myDiff \u003c 0)\n#\u003e  [1]  1  2  4  5  7  8  9 11 14 16 17 19 21 22 24 26 28 29 31 33 37 38 40 41 43\n#\u003e [26] 45 47 48 50 51 52 54 55 56 58 59 61 63 65 67 68 69 71 74 76 79 80 83 85 88\n#\u003e [51] 89 91 93 94 96 98 99\n\n## N.B. The first and second elements are incorrect order (among others)\nhead(testBaseSort)\n#\u003e Big Integer ('bigz') object of length 6:\n#\u003e [1] 100000000000000000000000000000000000000000000000000000000000000000000000000000073\n#\u003e [2] 100000000000000000000000000000000000000000000000000000000000000000000000000000057\n#\u003e [3] 100000000000000000000000000000000000000000000000000000000000000000000000000000046\n#\u003e [4] 100000000000000000000000000000000000000000000000000000000000000000000000000000095\n#\u003e [5] 100000000000000000000000000000000000000000000000000000000000000000000000000000081\n#\u003e [6] 100000000000000000000000000000000000000000000000000000000000000000000000000000058\n```\n\n## Acknowledgments and Resources\n\n  * Credit to [primo](\u003chttps://codegolf.stackexchange.com/users/4098/primo\u003e) (Mike Tryczak) and his excellent answer to [Fastest semiprime factorization](\u003chttps://codegolf.stackexchange.com/a/9088/52987\u003e).\n\n  * [Factoring large numbers with quadratic sieve](\u003chttps://blogs.msdn.microsoft.com/devdev/2006/06/19/factoring-large-numbers-with-quadratic-sieve/\u003e) on MSDN Archive.\n\n  * A really nice concise example is given here: [Factorization of *n = 87463* with the Quadratic Sieve](\u003chttps://www.math.colostate.edu/~hulpke/lectures/m400c/quadsievex.pdf\u003e)\n\n  * [Smooth numbers and the quadratic sieve](\u003chttp://library.msri.org/books/Book44/files/03carl.pdf\u003e) by Carl Pomerance\n\n  * [Implementing the Elliptic Curve Method of Factoring in Reconfigurable Hardware](\u003chttps://www.iacr.org/archive/ches2006/10/10.pdf\u003e) by Gaj K. et al.\n\n  * [Integer Factorization using the Quadratic Sieve](\u003chttps://micsymposium.org/mics_2011_proceedings/mics2011_submission_28.pdf\u003e) by Chad Seibert\n\n  * In the stackoverflow question and answer [What is the most efficient factoring algorithm for quadratic sieve extraction phase?](\u003chttps://stackoverflow.com/q/63541365/4408538\u003e) by [Ilya Gazman](\u003chttps://github.com/gazman-sdk\u003e), an efficient method for checking divisibility is sketched out that utilizes built-in types. You can see more on a video Ilya put on youtube: [E15: Quadratic Sieve Running on Java - Receiving](\u003chttps://youtu.be/sXg_WrCUX-Q\u003e). While `mpz_divisible_ui_p` is very efficient, we found better performance using this method.\n\n  * [R Function for returning ALL factors](\u003chttps://stackoverflow.com/a/49742904/4408538\u003e)\n\n  * [Issues factoring large number that is 99 digits long](\u003chttps://stackoverflow.com/a/66128627/4408538\u003e)\n\n\n## Current Research\n\nCurrently, our main focus for version `1.2.0` will be implementing the self initializing quadratic sieve.\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/RcppBigIntAlgos/issues\u003e).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwood000%2Frcppbigintalgos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwood000%2Frcppbigintalgos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwood000%2Frcppbigintalgos/lists"}