{"id":38489163,"url":"https://github.com/popojan/egypt","last_synced_at":"2026-01-17T05:40:07.150Z","repository":{"id":177080892,"uuid":"659920479","full_name":"popojan/egypt","owner":"popojan","description":"egyptian fractions with small denominators","archived":false,"fork":false,"pushed_at":"2025-12-05T18:18:56.000Z","size":1840,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-09T06:44:20.994Z","etag":null,"topics":["egyptian-fractions","rational-numbers","rationalization","sqrt"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/popojan.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2023-06-28T21:23:43.000Z","updated_at":"2025-12-05T18:19:01.000Z","dependencies_parsed_at":"2024-05-29T23:41:20.861Z","dependency_job_id":"b10beba8-bd4c-4dd2-9d8d-451ae73fbdf1","html_url":"https://github.com/popojan/egypt","commit_stats":null,"previous_names":["popojan/egypt"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/popojan/egypt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/popojan%2Fegypt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/popojan%2Fegypt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/popojan%2Fegypt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/popojan%2Fegypt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/popojan","download_url":"https://codeload.github.com/popojan/egypt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/popojan%2Fegypt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28500642,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T04:31:57.058Z","status":"ssl_error","status_checked_at":"2026-01-17T04:31:45.816Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["egyptian-fractions","rational-numbers","rationalization","sqrt"],"created_at":"2026-01-17T05:40:06.829Z","updated_at":"2026-01-17T05:40:07.130Z","avatar_url":"https://github.com/popojan.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Egyptian Fractions\n\nFast algorithm for representing rational numbers as egyptian fractions.\n\n## Properties\n* suitable for very large inputs\n* returns rather small denominators\n\n## Usage\n\n```\nEgyptian Fractions\n\nUsage: egypt [OPTIONS] [NUMERATOR] [DENOMINATOR]\n\nArguments:\n  [NUMERATOR]    [default: 1]\n  [DENOMINATOR]  [default: 1]\n\nOptions:\n  -r, --reverse        Reverse merge strategy\n  -m, --merge          Extra O(n^2) merge step possibly reducing number of terms\n      --raw            Output minimal number of raw quadruplets (aka symbolic sums)\n      --bisect         Output raw quadruplets bisected according to --limit\n  -s, --silent         No output\n      --batch          Batch mode (expects numerator and denominator on each line of stdin)\n  -l, --limit \u003cLIMIT\u003e  Maximum number of terms for breaking large symbolic sums [default: 8]\n  -h, --help           Print help\n  -V, --version        Print version\n```\n\n## Performance\n```\n$ time ./egypt -s '2 9689 ^ 1 -' '2 9941 ^ 1 -'\n\nreal    0m0.345s\nuser    0m0.296s\nsys     0m0.047s\n```\n\n```\n$ time ./egypt -s 162259276829213363391578010288127 170141183460469231731687303715884105727\n\nreal    0m0.002s\nuser    0m0.001s\nsys     0m0.001s\n```\n\n```\n$ time ./egypt --limit 2 999999 1000000\n1       2\n1       4\n1       8\n1       16\n1       33\n1       50\n1       83\n1       12450\n1       32912\n1       49368\n1       90387\n1       285716\n1       571432\n1       1999996\n1       685610442\n1       2057423870\n1       11904714285\n1       83332333335\n1       499999000000\n\nreal    0m0.002s\nuser    0m0.000s\nsys     0m0.002s\n```\n\n---\n\n## Examples\n\n### 7 / 19\n\n* Wolfram|Alpha\n  * 1 / 3 + 1 / 29 + 1 / 1653   \n* `egypt --merge --limit 19 7 19`\n  * 1 / 3 + 1 / 33 + 1 / 209    \n\n### 2023 / 2024\n* Wolfram|Alpha\n  * 1 / 2 + 1 / 3 + 1 / 7 + 1 / 43 + 1 / 16768 + 1 / 766160103 + 1 / 978335504948790912\n* `egypt --merge --limit 2023 2023 2024`\n  * 1 / 2 + 1 / 3 + 1 / 8 + 1 / 33 + 1 / 92\n* `egypt --reverse --merge --limit 2023 2023 2024`\n  * 1 / 2 + 1 / 3 + 1 / 7 + 1 / 43 + 1 / 18447 + 1 / 184184\n* `egypt --limit 2 2023 2024`\n    *   1 / 2 + 1 / 4 + 1 / 8 + 1 / 11 + 1 / 33 + 1 / 674 + 1 / 899 + 1 / 2442 + 1 / 4044 + 1 / 24938 + 1 / 2046264 + 1 / 2423704\n\n## Irrational / Transcendental Numbers\n\nSupports RPN expressions with constants: `pi`, `e`, `phi` (golden ratio), `sqrt2`, `gamma` (Euler-Mascheroni).\n\n```bash\n# Pi/4 as Egypt fractions (raw symbolic tuples)\n$ egypt pi 4 --raw -p 64\n1\t1\t1\t3\n4\t5\t1\t1\n9\t14\t1\t15\n219\t452\t1\t72\n32763\t33215\t1\t9\n331698\t364913\t1\t17\n6535219\t6900132\t1\t2\n20335483\t27235615\t1\t5\n```\n\nEach tuple `(u, v, i, j)` represents: `sum_{k=i}^{j} 1/((u-v+vk)(u+vk))`\n\nThe `-p` flag controls precision in bits (default 256). Higher precision = more CF terms = more tuples.\n\n```bash\n# Golden ratio\n$ egypt phi 1 --raw -p 64 | head -5\n1\t0\t0\t0\n1\t1\t1\t1\n1\t2\t1\t1\n2\t3\t1\t1\n3\t5\t1\t1\n```\n\nNote: For irrationals, output represents a finite-precision rational approximation.\nEarly tuples are stable convergents of the true constant; later ones may diverge.\n\n### Pell Equation Solver\n\nFind solutions to Pell equation p² - D·q² = ±1 using `--pell` flag:\n\n```bash\n# sqrt(13): fundamental solution 649² - 13·180² = 1\n$ egypt \"13 sqrt\" 1 --pell -p 64\nq\tp\tnorm\n1\t3\t-4\n...\n180\t649\t1\n# Fundamental solution (norm=1): p=649, q=180\n\n# Cattle problem (D=4729494)\n$ egypt \"4729494 sqrt\" 1 --pell -p 512 | tail -1\n50549485234315033074477819735540408986340\t109931986732829734979866232821433543901088049\t1\n```\n\nEgypt tuples encode CF convergent denominators directly, making Pell extraction\na byproduct of the representation. Precision (`-p`) must be sufficient for the\nCF period length; use higher values for larger D.\n\n## Note\n\n\u003e * returns rather small denominators\n\u003e\nWhen using legacy configuration `egypt --merge --limit \u003cLIMIT\u003e \u003cNUMERATOR\u003e \u003cDENOMINATOR\u003e`, where `LIMIT \u003e= DENOMINATOR - 1`,\nlargest denominator factor should not be greater than original denominator. Fast default limit is however `2`,\nwhich means that *bisecting* large symbolic sums can introduce bigger denominators. Moreover, `--limit` argument is itself\nlimited by `usize`, as opposed to other `BigInt` inputs.\n\n## Relation to Continued Fractions\n\n```\n\u003c\u003c \"wl/Egypt.wl\"\n\ncompare[Rational[p_, q_]] := {\n  Total /@ Partition[ Differences @ Convergents[ p/q ], 2],\n  ReleaseHold @ EgyptianFractions[ p/q , Method -\u003e \"Expression\"]\n}\n```\n\n**Theorem**: Egypt values equal paired differences of continued fraction convergents.\nThis explains the monotonicity property: paired CF differences cancel the alternating sign pattern.\n\n---\n\n## Theory \u0026 Related Work\n\nFor theoretical background on the symbolic telescoping representation:\n\n- **Paper**: [Egyptian Fractions via Modular Inverse: Symbolic Telescoping Representation](https://github.com/popojan/orbit/blob/main/docs/papers/egyptian-fractions-telescoping.tex)\n- **Wolfram implementation**: [`Orbit/Kernel/EgyptianFractions.wl`](https://github.com/popojan/orbit/blob/main/Orbit/Kernel/EgyptianFractions.wl)\n- **CF-Egypt Bijection (Proven Dec 2025)**: [Session documentation](https://github.com/popojan/orbit/blob/main/docs/sessions/2025-12-10-cf-egypt-equivalence/README.md)\n- **γ-Egypt Simplification**: [Characterization theorems](https://github.com/popojan/orbit/blob/main/docs/sessions/2025-12-10-cf-egypt-equivalence/gamma-egypt-simplification.md)\n\nThe symbolic representation `{u, v, i, j}` compresses consecutive unit fractions into\ntelescoping sums with closed form: `(j-i+1) / ((u-v+vi)(u+vj))`.\n\nThis reduces complexity from O(numerator) expanded fractions to O(log denominator) symbolic tuples.\n\n### CF-Egypt Bijection (✅ Proven)\n\nFor q = a/b with CF [0; a₁, a₂, ..., aₙ] and convergent denominators {q₀=1, q₁, ..., qₙ=b}:\n\n| Case | u_k | v_k | j_k |\n|------|-----|-----|-----|\n| Regular (k \u003c ⌈n/2⌉ or n even) | q_{2k-2} | q_{2k-1} | a_{2k} |\n| Last tuple, odd CF | q_{n-1} | q_n - q_{n-1} | 1 |\n\n**Key insight:** XGCD quotients ARE CF coefficients, enabling single-pass computation.\n\n---\n\n## Illustrations\n\n* Lissajous Curves: https://mathworld.wolfram.com/LissajousCurve.html\n\n![lissajous1](doc/7_11.png)\n\n![lissajous2](doc/8_11.png)\n\n![lissajous2](doc/53_57_83.png)\n\n## Approximating rational numbers\n\nby different rational numbers with denominator coprime to the original denominator\n\n### 7 / 11\n![approx_7_11](doc/approx_7_11.png)\n\n### 7 / 11 errors\n![approx_7_11_err](doc/approx_7_11_err.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpopojan%2Fegypt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpopojan%2Fegypt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpopojan%2Fegypt/lists"}