{"id":16661409,"url":"https://github.com/bodigrim/integer-roots","last_synced_at":"2026-03-06T18:10:11.175Z","repository":{"id":62436264,"uuid":"239174467","full_name":"Bodigrim/integer-roots","owner":"Bodigrim","description":"Integer roots and perfect powers of arbitrary precision","archived":false,"fork":false,"pushed_at":"2024-10-14T22:22:51.000Z","size":122,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T20:51:13.511Z","etag":null,"topics":["arbitrary-precision","integer-arithmetic","perfect-powers"],"latest_commit_sha":null,"homepage":"http://hackage.haskell.org/package/integer-roots","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bodigrim.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-08T17:34:26.000Z","updated_at":"2024-10-14T22:22:55.000Z","dependencies_parsed_at":"2025-02-15T14:42:15.019Z","dependency_job_id":null,"html_url":"https://github.com/Bodigrim/integer-roots","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodigrim%2Finteger-roots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodigrim%2Finteger-roots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodigrim%2Finteger-roots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bodigrim%2Finteger-roots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bodigrim","download_url":"https://codeload.github.com/Bodigrim/integer-roots/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248092898,"owners_count":21046578,"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","integer-arithmetic","perfect-powers"],"created_at":"2024-10-12T10:34:52.997Z","updated_at":"2026-03-06T18:10:11.162Z","avatar_url":"https://github.com/Bodigrim.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# integer-roots [![Hackage](http://img.shields.io/hackage/v/integer-roots.svg)](https://hackage.haskell.org/package/integer-roots) [![Stackage LTS](http://stackage.org/package/integer-roots/badge/lts)](http://stackage.org/lts/package/integer-roots) [![Stackage Nightly](http://stackage.org/package/integer-roots/badge/nightly)](http://stackage.org/nightly/package/integer-roots)\n\nCalculating integer roots and testing perfect powers of arbitrary precision.\n\n## Integer square root\n\nThe [integer square root](https://en.wikipedia.org/wiki/Integer_square_root)\n(`integerSquareRoot`)\nof a non-negative integer\n$n$\nis the greatest integer\n$m$\nsuch that\n$m\\le\\sqrt{n}$.\nAlternatively, in terms of the\n[floor function](https://en.wikipedia.org/wiki/Floor_and_ceiling_functions),\n$m = \\lfloor\\sqrt{n}\\rfloor$.\n\nFor example,\n\n```haskell\n\u003e integerSquareRoot 99\n9\n\u003e integerSquareRoot 101\n10\n```\n\nIt is tempting to implement `integerSquareRoot` via `sqrt :: Double -\u003e Double`:\n\n```haskell\nintegerSquareRoot :: Integer -\u003e Integer\nintegerSquareRoot = truncate . sqrt . fromInteger\n```\n\nHowever, this implementation is faulty:\n\n```haskell\n\u003e integerSquareRoot (3037000502^2)\n3037000501\n\u003e integerSquareRoot (2^1024) == 2^1024\nTrue\n```\n\nThe problem here is that `Double` can represent only\na limited subset of integers without precision loss.\nOnce we encounter larger integers, we lose precision\nand obtain all kinds of wrong results.\n\nThis library features a polymorphic, efficient and robust routine\n`integerSquareRoot :: Integral a =\u003e a -\u003e a`,\nwhich computes integer square roots by\n[Karatsuba square root algorithm](https://hal.inria.fr/inria-00072854/PDF/RR-3805.pdf)\nwithout intermediate `Double`s.\n\n## Integer cube roots\n\nThe integer cube root\n(`integerCubeRoot`)\nof an integer\n$n$\nequals to\n$\\lfloor\\sqrt[3]{n}\\rfloor$.\n\nAgain, a naive approach is to implement `integerCubeRoot`\nvia `Double`-typed computations:\n\n```haskell\nintegerCubeRoot :: Integer -\u003e Integer\nintegerCubeRoot = truncate . (** (1/3)) . fromInteger\n```\n\nHere the precision loss is even worse than for `integerSquareRoot`:\n\n```haskell\n\u003e integerCubeRoot (4^3)\n3\n\u003e integerCubeRoot (5^3)\n4\n```\n\nThat is why we provide a robust implementation of\n`integerCubeRoot :: Integral a =\u003e a -\u003e a`,\nwhich computes roots by\n[generalized Heron algorithm](https://en.wikipedia.org/wiki/Nth_root_algorithm).\n\n## Higher powers\n\nIn spirit of `integerSquareRoot` and `integerCubeRoot` this library\ncovers the general case as well, providing\n`integerRoot :: (Integral a, Integral b) =\u003e b -\u003e a -\u003e a`\nto compute integer $k$-th roots of arbitrary precision.\n\nThere is also `highestPower` routine, which tries hard to represent\nits input as a power with as large exponent as possible. This is a useful function\nin number theory, e. g., elliptic curve factorisation.\n\n```haskell\n\u003e map highestPower [2..10]\n[(2,1),(3,1),(2,2),(5,1),(6,1),(7,1),(2,3),(3,2),(10,1)]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbodigrim%2Finteger-roots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbodigrim%2Finteger-roots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbodigrim%2Finteger-roots/lists"}