{"id":18425736,"url":"https://github.com/dwayne/elm-natural","last_synced_at":"2025-04-07T16:32:00.239Z","repository":{"id":65386392,"uuid":"578987843","full_name":"dwayne/elm-natural","owner":"dwayne","description":"The natural numbers, ℕ = { 0, 1, 2, ... }.","archived":false,"fork":false,"pushed_at":"2024-07-31T13:58:32.000Z","size":195,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T21:24:45.150Z","etag":null,"topics":["arbitrary-precision","bignum","elm","extended-precision","multiple-precision","natural-numbers"],"latest_commit_sha":null,"homepage":"https://package.elm-lang.org/packages/dwayne/elm-natural/latest/","language":"Elm","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dwayne.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.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}},"created_at":"2022-12-16T11:29:23.000Z","updated_at":"2024-07-31T13:58:35.000Z","dependencies_parsed_at":"2023-12-03T16:22:48.016Z","dependency_job_id":"5b29dadd-28d9-483b-a85d-3b342fa13bb4","html_url":"https://github.com/dwayne/elm-natural","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-natural","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-natural/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-natural/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwayne%2Felm-natural/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dwayne","download_url":"https://codeload.github.com/dwayne/elm-natural/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247688018,"owners_count":20979581,"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":["arbitrary-precision","bignum","elm","extended-precision","multiple-precision","natural-numbers"],"created_at":"2024-11-06T05:05:14.332Z","updated_at":"2025-04-07T16:31:59.793Z","avatar_url":"https://github.com/dwayne.png","language":"Elm","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elm Natural\n\nA pure Elm library for computing with the [natural numbers](https://en.wikipedia.org/wiki/Natural_number),\nℕ = { 0, 1, 2, ... }.\n\n## What's available?\n\n### The natural numbers from 0 to 10\n\n- `zero`\n- `one`\n- `two`\n- `three`\n- `four`\n- `five`\n- `six`\n- `seven`\n- `eight`\n- `nine`\n- `ten`\n\n### Ways to create natural numbers from an `Int`\n\n- `fromSafeInt`\n- `fromInt`\n\n### Ways to create natural numbers from a `String`\n\n- `fromSafeString`\n- `fromString` (supports binary, octal, decimal, and hexadecimal inputs)\n- `fromBinaryString`\n- `fromOctalString`\n- `fromDecimalString`\n- `fromHexString`\n- `fromBaseBString`\n\n### Comparison operators\n\n- `==`\n- `/=`\n- `compare`\n- `isLessThan`\n- `isLessThanOrEqual`\n- `isGreaterThan`\n- `isGreaterThanOrEqual`\n- `max`\n- `min`\n\n### Predicates for classification\n\n- `isZero` (i.e. `== 0`)\n- `isOne` (i.e. `== 1`)\n- `isPositive` (i.e. `\u003e 0`)\n- `isEven`\n- `isOdd`\n\n### Arithmetic\n\n- `add`\n- `sub` (saturating subtraction)\n- `mul`\n- `divModBy` (Euclidean division)\n- `divBy`\n- `modBy`\n- `exp`\n\n### A way to convert to an `Int`\n\n- `toInt` (use with caution since it discards information)\n\n### Ways to convert to a `String`\n\n- `toString` (same as `toDecimalString`)\n- `toBinaryString`\n- `toOctalString`\n- `toDecimalString`\n- `toHexString`\n- `toBaseBString`\n\n## Examples\n\n### Factorial\n\nThe [factorial](https://en.wikipedia.org/wiki/Factorial) of a natural number `n`, denoted by `n!`, is the product of all\npositive natural numbers less than or equal to `n`. The value of `0!` is `1` by convention.\n\n```elm\nimport Natural as N exposing (Natural)\n\nfact : Natural -\u003e Natural\nfact n =\n    if N.isZero n then\n      N.one\n\n    else\n      N.mul n \u003c| fact (N.sub n N.one)\n```\n\n#### What's `100!`?\n\n```elm\nN.toString (fact (N.fromSafeInt 100)) == \"93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000\"\n```\n\n### More examples\n\nA few more examples can be found in the `examples/src` directory.\n\n- `Fibonacci.elm` - Computes up to the 1000th [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number).\n- `Pi.elm` - Computes the first 100 digits of [`π`](https://en.wikipedia.org/wiki/Pi).\n- `E.elm` - Computes the first 100 digits of [`e`](https://en.wikipedia.org/wiki/E_%28mathematical_constant%29).\n- `BaseConversion.elm` - Converts decimal numbers to their binary, octal, and hexadecimal representations.\n\n## Performance\n\nThis library tries to provide a reasonably efficient implementation of extended-precision\narithmetic over the natural numbers while being written purely in Elm and making extensive\nuse of lists. Within that context it gives reasonable performance.\n\n```txt\nNatural / comparison\n\n  compare (2^26)^9999-1 and (2^26)^9999-2\n  runs/second     = 5,839\n  goodness of fit = 99.86%\n\nNatural / multiplication\n\n  999..9 (100 9's) * 999..9 (100 9's)\n  runs/second     = 53,038\n  goodness of fit = 99.79%\n\nNatural / division with remainder\n\n  (999..9 (100 9's))^2 / 999..9 (100 9's)\n  runs/second     = 6,852\n  goodness of fit = 99.91%\n\nNatural / exponentiation\n\n  2 ^ 1000\n  runs/second     = 15,623\n  goodness of fit = 99.7%\n```\n\n**N.B.** *You can read `benchmarks/README.md` to learn how to reproduce the above benchmark report and\nto see how this library compares against [`cmditch/elm-bigint`](https://package.elm-lang.org/packages/cmditch/elm-bigint/2.0.1/).*\n\n## Resources\n\n- Chapter 17 - Extended-Precision Arithmetic of [C Interfaces and Implementations: Techniques for Creating Reusable Software](https://archive.org/details/cinterfacesimple0000hans) helped me to design, organize and implement the library.\n- [Lua Bint](https://github.com/edubart/lua-bint/tree/c73268472aa48554cf337c29b7550ce127f424a8#examples) inspired the examples.\n- [Per Brinch Hansen, Multiple-Length Division Revisited: A Tour of the Minefield](https://surface.syr.edu/cgi/viewcontent.cgi?article=1162\u0026context=eecs_techreports) helped me implement an efficient divide-and-correct algorithm for division with remainder.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwayne%2Felm-natural","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdwayne%2Felm-natural","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwayne%2Felm-natural/lists"}