{"id":16950170,"url":"https://github.com/sbplat/pell","last_synced_at":"2025-07-27T23:41:42.869Z","repository":{"id":161374680,"uuid":"510777342","full_name":"sbplat/pell","owner":"sbplat","description":"Find the first # of smallest positive integer pairs (x, y) that satisfy Pell's equation: x^2 - n * y^2 = 1.","archived":false,"fork":false,"pushed_at":"2024-01-13T17:24:40.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-26T07:07:36.601Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://sbplat.github.io/pell/","language":"JavaScript","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/sbplat.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}},"created_at":"2022-07-05T14:44:29.000Z","updated_at":"2024-03-14T05:47:05.000Z","dependencies_parsed_at":"2024-01-13T18:49:22.588Z","dependency_job_id":null,"html_url":"https://github.com/sbplat/pell","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbplat%2Fpell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbplat%2Fpell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbplat%2Fpell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbplat%2Fpell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sbplat","download_url":"https://codeload.github.com/sbplat/pell/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244776771,"owners_count":20508579,"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":"2024-10-13T21:57:00.186Z","updated_at":"2025-03-21T10:24:57.660Z","avatar_url":"https://github.com/sbplat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pell's Equation Solver\n\nFind the first # of smallest positive integer pairs $(x, y)$ that satisfy Pell's equation: $x^2 - ny^2 = 1$. Try it out [here](https://sbplat.github.io/pell/)!\n\n## How does it work?\n\nThe hardest part of this problem is finding the primitive solution (not the trivial one). Luckily, there is an algorithm known as the [Chakravala method](https://en.wikipedia.org/wiki/Chakravala_method) that can be used to find the primitive solution to Pell's equation.\n\nOnce we've found the primitive solution, we can generate all the other solutions really easily (in $O(1)$ time!).\nNotice that given a solution $(x_i, y_i)$, we have\n$$x_{i+1}+\\sqrt{n}y_{i+1}=(x_{i}+\\sqrt{n}y_{i})(x_0+\\sqrt{n}y_0)$$\nwhere $(x_0, y_0)$ is the primitive solution and $(x_{i+1}, y_{i+1})$ is the next solution. This can be proven by induction. Now, by comparing the coefficients of $1$ and $\\sqrt{n}$, we get the following recurrence relations:\n```math\n\\begin{align*}\n    x_{i+1} \u0026= x_0x_{i} + n y_0y_{i}\\\\\n    y_{i+1} \u0026= x_0y_{i} + y_0x_{i}\n\\end{align*}\n```\nWe can use this to generate any finite number of solutions to Pell's equation.\n\n## Why can't $n$ be a perfect square?\n\nAssume $x\\neq 1$ and $y\\neq 0$ since this is a trivial solution.\nLet's also assume $n$ is a perfect square, so $n = k^2$ for some positive integer $k$. We can rewrite Pell's equation as\n$$x^2 - k^2y^2 = 1\\implies x^2 - (ky)^2 = 1$$\nIt's easy to see that the only solution to this is $x = 1$ and $ky = 0$. But this contradicts our first assumption that $x\\neq 1$ and $y\\neq 0$, so $n$ cannot be a perfect square.\n\n## How do I run this from source?\n\n1. First, clone the repository and change into the directory:\n```sh\ngit clone https://github.com/sbplat/pell.git\ncd pell\n```\n2. Open `index.html` with your browser.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbplat%2Fpell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsbplat%2Fpell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbplat%2Fpell/lists"}