{"id":28426614,"url":"https://github.com/keep94/contfrac","last_synced_at":"2025-06-30T02:32:53.075Z","repository":{"id":147665283,"uuid":"62392128","full_name":"keep94/contfrac","owner":"keep94","description":"Python code to explore algebraic numbers written as continued fractions","archived":false,"fork":false,"pushed_at":"2025-03-22T16:23:31.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-05T11:11:34.031Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/keep94.png","metadata":{"files":{"readme":"README.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}},"created_at":"2016-07-01T12:40:29.000Z","updated_at":"2025-03-22T16:23:35.000Z","dependencies_parsed_at":"2023-06-09T08:45:52.366Z","dependency_job_id":null,"html_url":"https://github.com/keep94/contfrac","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/keep94/contfrac","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keep94%2Fcontfrac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keep94%2Fcontfrac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keep94%2Fcontfrac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keep94%2Fcontfrac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keep94","download_url":"https://codeload.github.com/keep94/contfrac/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keep94%2Fcontfrac/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262698948,"owners_count":23350207,"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":[],"created_at":"2025-06-05T11:10:42.055Z","updated_at":"2025-06-30T02:32:53.059Z","avatar_url":"https://github.com/keep94.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# contfrac\n\nPython code to explore algebraic numbers written as continued fractions\n\n# Introduction\n\nJust as rational numbers can evaluate to repeating decimals, irrational roots\nof quadratic equations evaluate to repeating continued fractions. These\nirrational roots of quadratic equations are known as algebraic numbers of\ndegree 2. In this document we simply call them algebraic numbers.\nThis repo provides tools for exploring the beautiful world of algebraic numbers\nand continued fractions.\n\n# Definitions\n\nWe refer to algebraic numbers of degree 2 as simply *algebraic numbers*\n\nAlgebraic numbers can be written as **(a + sqrt(r)) / b** where *a* and *b*\nare integers, b != 0, and r \u003e 0 and not a perfect square. For example in\n(-4 + sqrt(50)) / 7, r = 50, a = -4, b = 7\n\nSince we are studying continued fractions, the algebraic numbers we deal with\nfrom now on are between 0 and 1.\n\nWe say an algebraic number **(a + sqrt(r)) / b** is in *normalized* form if *b* divides *r - a^2* and *r*, *a*, and *b* are as small as possible.\n\nFor example (-3 + sqrt(31)) / 11 is in normalized form because 11 divides 22,\n31 - 3^2. However, (-2 + sqrt(11)) / 5 is not in normalized form because 5\ndoes not divide 7, 11 - 2^2. (-6 + sqrt(124)) / 22 is not in normilised form\neither because although 22 divides 88, (-6 + sqrt(124)) / 22 can be reduced to\n(-3 + sqrt(31)) / 11 which is in normalized form because 11 divides 22.\n\nAny algebraic number can be written in normalized form by multiplying\nits numberator and denominator by some constant.\nFor example, (-4 + sqrt(44)) / 10 is not in normalized form, but multiplying\nthe numerator and denominator by 2.5 gives (-10 + sqrt(275)) / 25 which is in\nnormalized form.\n\nWe say that an algebraic number is *strictly repeating* if its continued\nfraction has no non repeating pattern preceding the repeating pattern.\nFor example an algebraic number with continued fraction 3,5,3,5,... is\nstrictly repeating whereas an algebraic number with continued fraction\n1,2,4,3,5,3,5,... is not.\n\nWe claim that an algebraic number **(a + sqrt(r)) / b** is strictly repeating\nif and only if:\n\n1. b \u003e 0\n2. a \u003c 0\n3. (a + sqrt(r)) / b \u003c 1\n4. (-a + sqrt(r)) / b \u003e 1\n\nFor example, (-1 + sqrt(11)) / 4 is strictly repeating because 1 + sqrt(11) \u003e 4\nbut (-1 + sqrt(11)) / 5 is not because 1 + sqrt(11) \u003c 5.\n\nWe say that a strictly repeating algebraic number has *characteristic* of r if\nwhen written in normalized form, the number under the radical is r.\nFor example, the strictly repeating algebraic number (-3 + sqrt(37)) / 7,\nwhich is already in normalized form, has characteristic of 37.\nThe strictly repeating algebraic number (-2 + sqrt(11)) / 5 has characteristic\nof 275 because (-2 + sqrt(11)) / 5 in normalized form is (-10 + sqrt(275)) / 25.\n\nWe say that the set *C(n)* contains all the strictly repeating algebraic\nnumbers with characteristic n. For each n, the cardinality of C(n) is finite.\n\nWe say that the set *P(n)* is a partition of C(n) such that each element\nof P(n) contain algebraic numbers whose continued fractions all contain the\nsame repeating patterns. For instance, the continued fractions\n1,2,3,4,1,2,3,4,... and 2,3,4,1,2,3,4,1,... are both strictly repeating and\ncontain the repeating patterns 1,2,3,4; 2,3,4,1; 3,4,1,2; and 4,1,2,3\n\n# Observations\n\n1. ~~It seems that if n is prime and n mod 4 == 3, then all algebraic numbers\nin C(n) have the same repeating sequences in their continued fraction. That\nis the cardinality of P(n) is 1.~~ n = 79 is the smallest contridiction to\nthis conjecture. `len(cont.Partition(79))` is 3\n\n2. It seems that if n is prime and n mod 4 == 1\n(the equivalent of n is both prime and can be written as the sum of two squares)\nthen the elements of P(n) all have odd cardinality.\n\n3. If n is composite and can be written as the sum of two squares then the\nelements of P(n) can have even or odd cardinality.\n\n4. It seems that if n cannot be written as the sum of two squares then the\nelements of P(n) all have even cardinality.\n\n5. It seems that for any n, the cardinality of the elements of P(n) are either\nall even or all odd.\n\n\n# Getting started\n\nThis python repo now works with python 3.\n\n1. Download the cont.py file\n2. Start python by typing python at the mac os X prompt.\n\nThen...\n\n```\n\u003e\u003e\u003e import cont\n\u003e\u003e\u003e cont.Expand(7, 1, 4)\n([1], [10, 3, 2, 3])\n```\n\nThe continued fraction for (sqrt(7) + 1) / 4 is 1,10,3,2,3,10,3,2,3, ...\n\n# The Python Code\n\nThis guide assumes that algebraic numbers are of the form\n**(a + sqrt(r)) / b**. Unless explicitly stated, r must be a positive integer\nthat is not a perfect square, a must be an integer, and b must be an integer\nthat is not 0.\n\n## cont.AllWith(r)\n\nReturns all algebraic numbers with characteristic r. Returns the empty list\nif r is a perfect square.\n\n```\n\u003e\u003e\u003e cont.AllWith(31)\n[(31, -1, 5), (31, -1, 6), (31, -4, 3), (31, -4, 5), (31, -5, 1), (31, -5, 6), (31, -5, 2), (31, -5, 3)]\n```\n\n## cont.AllWithReduced(r)\n\nWorks like cont.AllWith except that it reduces the returned algebraic\nnumbers to lowest terms. Unlike cont.AllWith, the returned algebraic\nnumbers may not be in normalized form.\n\n```\n\u003e\u003e\u003e cont.AllWithReduced(24)\n[(6, -1, 2), (24, -2, 5), (24, -3, 3), (24, -3, 5), (24, -4, 1), (6, -2, 4)]\n```\n\n## cont.Characteristic(r, a, b)\n\nReturns the characteristic of the given algebraic number. Returns 0 if\ngiven algebraic number is not between 0 and 1 or is not strictly repeating.\n\n```\n\u003e\u003e\u003e cont.Characteristic(11, -2, 5)\n275\n```\n\n## cont.Expand(r, a, b)\n\nReturns the continued fraction of the given algebraic number in two parts.\nThe first part is the non repeating sequence; the second part is the repeating\nsequence.\n\n```\n\u003e\u003e\u003e cont.Expand(7, -1, 4)\n([2], [2, 3, 10, 3])\n```\n\nIn this exapmple, the continued fraction is 2,2,3,10,3,2,3,10,3, ...\n\n## cont.Normalize(r, a, b)\n\nReturns the given algebraic number in normalized form.\n\n```\n\u003e\u003e\u003e cont.Normalize(7, 1, 4)\n(28, 2, 8)\n```\n\n## cont.Partition(r)\n\nPartitions the algebraic numbers of characteristic *r* according to the\nrepeating sequence their continued fractions produce. If r is a perfect\nsquare, returns the empty list.\n\n```\n\u003e\u003e\u003e for group in cont.Partition(41):\n...   print('=' * 35)\n...   for x in group:\n...     print(x, cont.Expand(*x))\n... \n===================================\n(41, -3, 4) ([], [1, 5, 1, 2, 2])\n(41, -5, 8) ([], [5, 1, 2, 2, 1])\n(41, -5, 2) ([], [1, 2, 2, 1, 5])\n(41, -3, 8) ([], [2, 2, 1, 5, 1])\n(41, -5, 4) ([], [2, 1, 5, 1, 2])\n===================================\n(41, -4, 5) ([], [2, 12, 2])\n(41, -6, 5) ([], [12, 2, 2])\n(41, -6, 1) ([], [2, 2, 12])\n```\n\n## cont.PartitionReduced(r)\n\nWorks like cont.Partition except that it reduces the returned algebraic\nnumbers to lowest terms. Unlike cont.Partition, the returned\nalgebraic numbers may not be in normalized form.\n\n```\n\u003e\u003e\u003e for group in cont.PartitionReduced(24):\n...   print('=' * 35)\n...   for x in group:\n...     print(x, cont.Expand(*x))\n...\n===================================\n(6, -1, 2) ([], [1, 2, 1, 1])\n(24, -3, 5) ([], [2, 1, 1, 1])\n(24, -3, 3) ([], [1, 1, 1, 2])\n(24, -2, 5) ([], [1, 1, 2, 1])\n===================================\n(24, -4, 1) ([], [1, 8])\n(6, -2, 4) ([], [8, 1])\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeep94%2Fcontfrac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeep94%2Fcontfrac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeep94%2Fcontfrac/lists"}