{"id":24455238,"url":"https://github.com/antononcube/raku-math-numbertheory","last_synced_at":"2025-04-13T02:29:00.136Z","repository":{"id":271729200,"uuid":"914373084","full_name":"antononcube/Raku-Math-NumberTheory","owner":"antononcube","description":"Raku package with number theory functions.","archived":false,"fork":false,"pushed_at":"2025-04-05T17:31:24.000Z","size":99,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T18:27:37.486Z","etag":null,"topics":["euler-phi","modular-exponentiation","modular-inverse","number-theory","prime-factors","raku","rakulang","totient"],"latest_commit_sha":null,"homepage":"https://raku.land/zef:antononcube/Math::NumberTheory","language":"Raku","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antononcube.png","metadata":{"files":{"readme":"README-work.md","changelog":null,"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}},"created_at":"2025-01-09T13:22:45.000Z","updated_at":"2025-04-05T17:31:28.000Z","dependencies_parsed_at":"2025-01-27T19:45:49.917Z","dependency_job_id":"ac797f13-e5b7-4924-8a19-a9c4a5652464","html_url":"https://github.com/antononcube/Raku-Math-NumberTheory","commit_stats":null,"previous_names":["antononcube/raku-math-numbertheory"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-NumberTheory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-NumberTheory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-NumberTheory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-NumberTheory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antononcube","download_url":"https://codeload.github.com/antononcube/Raku-Math-NumberTheory/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657750,"owners_count":21140839,"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","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":["euler-phi","modular-exponentiation","modular-inverse","number-theory","prime-factors","raku","rakulang","totient"],"created_at":"2025-01-21T02:12:51.729Z","updated_at":"2025-04-13T02:29:00.128Z","avatar_url":"https://github.com/antononcube.png","language":"Raku","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Math::NumberTheory\n\nRaku package with Number theory functions.\n\nThe function names and features follow the \n[Number theory functions of Wolfram Language](http://reference.wolfram.com/language/guide/NumberTheory.html).\n\n**Remark:**  Raku has some nice built-in Number theory functions, like, `base`, `gcd`, `mod`, `polymod`, `expmod`, `is-prime`. \nThey somewhat lack generality, hence their functionalities are extended with this package. \nFor example, `is-prime` works with lists and [Gaussian integers](https://en.wikipedia.org/wiki/Gaussian_integer).\n\n-------\n\n## Installation\n\nFrom [Zef ecosystem](https://raku.land):\n\n```\nzef install Math::NumberTheory\n```\n\nFrom GitHub:\n\n```\nzef install https://github.com/antononcube/Raku-Math-NumberTheory\n```\n\n-------\n\n## Usage examples\n\n## GCD\n\nThe infix operator `gcd` for calculating the Greatest Common Divisor (GCD) \nis extended to work with rational numbers and [Gaussian integers](https://en.wikipedia.org/wiki/Gaussian_integer):\n\n### Rationals\n\nFor rational numbers `r1` and `r2`, `r1 gcd r2` gives the greatest rational number `r` for which `r1/r` and `r2/r` are integers.\n\n```perl6\nuse Math::NumberTheory;\n\u003c1/3\u003e gcd \u003c2/5\u003e gcd \u003c1/7\u003e\n==\u003e {.raku}()\n```\n\n### Gaussian integers\n\nGCD for two Gaussian integers (complex numbers with integer real and imaginary parts):\n\n```perl6\n(10 + 15i) gcd (-3 + 2i)\n```\n\n```perl6\n105 gcd (7 + 49i)\n```\n\nHere is verification of the latter:\n\n```perl6\nsay 105 / (7 + 14i);\nsay (7 + 49i) / (7 + 14i);\n```\n\n### Prime number testing\n\nThe built-in sub `is-prime` is extended to work with [Gaussian integers](https://en.wikipedia.org/wiki/Gaussian_integer):\n\n```perl6\nsay is-prime(2 + 1i);\nsay is-prime(5, :gaussian-integers);\n```\n\nAnother extension is threading over lists of numbers:\n\n```perl6\nis-prime(^6)\n```\n\n### Factor integers\n\nGives a list of the prime factors of an integer argument, together with their exponents:\n\n```perl6\nfactor-integer(factorial(20))\n```\n\n**Remark:** By default `factor-integer` uses [Pollard's Rho algorithm](https://en.wikipedia.org/wiki/Pollard's_rho_algorithm) --\nspecified with `method =\u003e 'rho'` --\nas implemented at RosettaCode, [RC1], with similar implementations provided by \"Prime::Factor\", [SSp1], and \"Math::Sequences\", [RCp1].\n\nDo partial factorization, pulling out at most `k` distinct factors:\n\n```perl6\nfactor-integer(factorial(20), 3, method =\u003e 'trial')\n```\n\n\n### Chinese reminders\n\nData:\n\n```perl6\nmy @data = 931074546, 117172357, 482333642, 199386034, 394354985;\n```\n\nKeys:\n\n```perl6\n#my @keys = random-prime(10**9 .. 10**12, @data.elems);\nmy @keys = 274199185649, 786765306443, 970592805341, 293623796783, 238475031661;\n```\n\n**Remark:** Using these larger keys is also a performance check.\n\n\nEncrypted data:\n\n```perl6\nmy $encrypted = chinese-reminder(@data, @keys);\n```\n\nDecrypted:\n\n```perl6\nmy @decrypted = @keys.map($encrypted mod *);\n```\n\n### Modular exponentiation and modular inversion\n\nThe sub `power-mod` extends the built-in sub `expmod`.\nThe sub `modular-inverse` is based on `power-mod`.\n\n`expmod` gives an error and no result when the 1st argument cannot be inverted with the last argument:\n\n```perl6\nexpmod(30, -1, 12)\n```\n\n`power-mod` returns `Nil`:\n\n```perl6\npower-mod(30, -1, 12).defined\n```\n\n\n### Number-base related\n\nThere are several subs that provide functionalities related to number systems representation.\n\nFor example, here we find the digit-breakdown of $100!$:\n\n```perl6\n100.\u0026factorial.\u0026digit-count\n```\n\nHere is an example of using `real-digits`:\n\n```perl6\nreal-digits(123.55555)\n```\n\nNon-integer bases can be also used:\n\n```perl6\nmy $r = real-digits(π, ϕ);\n```\n\nHere we recover $\\pi$ from the Golden ratio representation obtained above:\n\n```perl6\n$r.head.kv.map( -\u003e $i, $d { $d * ϕ ** ($r.tail - $i - 1)  }).sum.round(10e-12);\n```\n\nThe sub `phi-number-system` can be used to compute\n[Phi number system](https://mathworld.wolfram.com/PhiNumberSystem.html)\nrepresentations:\n\n```perl6\n.say for (^7)».\u0026phi-number-system\n```\n\n**Remark:** Because of the approximation of the Golden ratio used in “Math::NumberTheory”,\nin order to get exact integers from phi-digits we have to round using small multiples of 10. \n\n-------\n\n## TODO\n\n- [ ] TODO Implementation\n  - [X] DONE Gaussian integers GCD\n  - [X] DONE Gaussian integers factorization\n  - [X] DONE Moebius Mu function, Liouville lambda function \n    - [X] DONE Integers\n    - [X] DONE Gaussian integers\n  - [X] DONE Square-free test\n    - [X] DONE Integers\n    - [X] DONE Gaussian integers\n  - [X] DONE Rational numbers GCD\n  - [X] DONE Multiplicative Order\n  - [X] DONE Gaussian integers LCM \n  - [X] DONE Rational numbers LCM\n  - [X] DONE Carmichael lambda\n  - [ ] TODO Integer partitions\n  - [ ] TODO Sum of squares representation\n  - [ ] TODO Figure out which memoization approach to use:\n    - [ ] Via the package [\"Memoize\"](https://raku.land/zef:lizmat/Memoize)\n    - [ ] Via `use experimental :cached` and `sub blah(...) is cached {...}` \n  - [ ] TODO CLI\n- [ ] TODO Documentation\n  - [ ] TODO Blog post on first non-zero digit of 10_000!\n  - [ ] TODO Videos\n    - [X] DONE [Neat examples 1](https://www.youtube.com/watch?v=wXXWyRAAPvc)\n    - [ ] TODO Neat examples 2\n    - [ ] TODO Neat examples 3\n\n-------\n\n## References\n\n### Articles, blog posts, wiki-pages\n\n[RC1] Rosetta Code, [Prime decomposition](https://rosettacode.org/wiki/Prime_decomposition),\n[Section \"Pure Raku\"](https://rosettacode.org/wiki/Prime_decomposition#Pure_Raku).\n\n### Packages \n\n[RCp1] Raku Community,\n[Math::Sequences Raku package](https://github.com/raku-community-modules/Math-Sequences),\n(2016-2024),\n[GitHub/raku-community-modules](https://github.com/raku-community-modules).\n\n[SSp1] Stephen Schulze,\n[Prime::Factor Raku package](https://github.com/thundergnat/Prime-Factor),\n(2016-2023),\n[GitHub/thundergnat](https://github.com/thundergnat).\n\n### Videos\n\n[AAv1] Anton Antonov,\n[\"Number theory neat examples in Raku (Set 1)\"](https://www.youtube.com/watch?v=wXXWyRAAPvc),\n(2025),\n[YouTube/@AAA4prediction](https://www.youtube.com/@AAA4prediction).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantononcube%2Fraku-math-numbertheory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantononcube%2Fraku-math-numbertheory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantononcube%2Fraku-math-numbertheory/lists"}