Negative Pell equation solver
Find out whether x² − Dy² = −1 has whole-number solutions and get the smallest one: when D is 13, the answer is x 18 and y 5, found from a continued fraction with an odd period of 5. Even very large solutions come out exact. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- D
- 13
- D
- 13
- Period length
- 5
- X
- 18
- Y
- 5
The negative Pell equation solver determines whether x squared minus D times y squared equals minus one has an integer solution for a positive non-square integer D. When the equation is solvable, it returns the smallest positive x and y exactly, without floating-point rounding. It also reports the continued-fraction period length that decides solvability.
What the negative Pell equation asks
The equation x² − Dy² = −1 looks like a small variation of the ordinary Pell equation, yet that change of sign makes existence conditional. Here D must be a positive integer that is not a perfect square, while x and y are sought among positive integers. Other non-square values have no solution at all.
This solver separates those cases and, when a solution exists, identifies the minimal one, meaning the solution with the least positive x. Because Pell solutions can grow far beyond the safe numeric range of JavaScript and many databases, x, y, and D are returned as decimal strings. Those strings are exact integers, not approximations.
The solvable flag gives the direct decision, and solution fields are omitted when the decision is false, avoiding misleading null values or invented placeholders. A perfect-square D is rejected because it falls outside the Pell-equation domain specified by this tool.
How continued fractions decide solvability
The calculation uses the periodic simple continued fraction of the square root of D. For every positive non-square integer, that expansion has a finite repeating block. A classical theorem gives a precise test: the negative Pell equation is solvable exactly when the repeating block has odd length.
The solver generates the expansion with integer recurrence relations only; it never estimates the square root with floating-point arithmetic. At each convergent it tracks numerator and denominator as arbitrary-precision integers and checks the defining equation directly. If a convergent yields −1, its numerator and denominator are the minimal positive x and y.
If the period closes without such a convergent, the period length is even and the response states that no integer solution exists. This approach provides both an auditable reason for the decision and the requested solution in one pass. It is deterministic: identical input always follows the same recurrence and produces identical output, with no network calls, random choices, clock reads, or dependence on locale-specific number formatting.
Reading and using the result safely
Supply D through the d field as an integer. You can verify it independently by parsing those strings with a big-integer library and checking x*x − D*y*y = −1. An unsolvable result includes the same D and period length but omits x and y, because no values could satisfy the contract.
This distinction is convenient in research scripts, classroom demonstrations, and number-theory pipelines: branch on solvable before reading the solution fields. Invalid values, including fractions, negative integers, missing input, and perfect squares, produce a typed input error rather than a mathematical answer.
What you can do with it
Check a number-theory conjecture
Test selected non-square values of D and capture the exact minimal solutions for further symbolic analysis.
Prepare worked examples
Generate reproducible solvable and unsolvable cases for lessons on quadratic irrationals and continued fractions.
Validate another implementation
Compare a custom Pell solver against deterministic exact results, including period lengths and very large decimal answers.
FAQ
When is the negative Pell equation solvable?
It is solvable exactly when the periodic continued fraction of the square root of D has odd period length.
What does minimal solution mean?
It is the positive integer solution with the smallest x; the corresponding positive y is determined with it.
Does solvable false mean the search stopped early?
No. It means the continued-fraction period closed with even length, which proves that no integer solution exists.
What is the smallest solution when D is 13?
It is x 18 and y 5. The continued fraction of the square root of 13 repeats with a period of 5, an odd length, so a solution exists, and 18 and 5 are the smallest positive pair.
How is this different from the ordinary Pell equation?
Only the sign changes, from +1 to −1, yet that makes solutions conditional. Some non-square values of D have a solution and others have none at all, so the answer first tells you which case you are in.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/numth/negative-pell \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"d":13}'const res = await fetch("https://api.kit.forhosting.com/numth/negative-pell", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"d": 13
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/negative-pell",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"d": 13
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/negative-pell", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"d":13}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"d":13}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/negative-pell", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"d": 13
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.negative_pell",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Large answers are emitted as decimal strings, so every digit survives transport through JSON, browsers, command-line tools, and application code.
What the negative Pell equation asks
Some values work immediately: for D = 2, the pair x = 1 and y = 1 satisfies the equation.
Reading and using the result safely
API callers that may exceed the safe integer range of their programming language should send an exact unsigned decimal string; the computational core accepts that representation without discarding digits. A successful result includes solvable set to true, the continued-fraction period_length, and exact decimal strings x and y. The browser and API paths share the same pure solver, so interactive exploration and automated calls apply the same validation, recurrence, minimality rule, and exact-output representation. API use is charged at $0.002 per request.
Technical questions
Why are x and y returned as strings?
Pell solutions can exceed ordinary JSON and JavaScript integer precision. Decimal strings preserve every digit exactly.
What happens when D is a perfect square?
The request fails with an invalid_input error because this capability is defined only for non-square D.
How much does an API request cost?
Each API request costs $0.002; the same deterministic solver can also run free in the browser.