ForHosting KIT · Developer Utilities

Pell equation solver

The Pell equation solver finds the fundamental solution of x² − Dy² = 1 for a positive integer D that is not a perfect square.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

Enter D and receive the smallest positive integer pair x and y that satisfies the equation. The calculation uses exact integer arithmetic, so even answers beyond JavaScript's ordinary numeric range remain accurate. It is useful for number theory exercises, continued-fraction study, algorithm checks, and software that needs a dependable canonical Pell solution.

What the fundamental solution means

For a fixed positive nonsquare integer D, the Pell equation asks for integer values satisfying x² − Dy² = 1. There are infinitely many positive solutions, but they are generated from one smallest nontrivial pair. This calculator returns that pair, called the fundamental solution: x and y are positive, the identity holds exactly, and no smaller positive x solves the same equation. For example, D = 2 produces x = 3 and y = 2 because 3² − 2 × 2² equals 1. The input must be a whole number from 2 through 10,000,000. A perfect-square D is rejected because x² − Dy² then factors as (x − √D y)(x + √D y), and there is no positive solution with y greater than zero. Results are decimal strings rather than floating-point values. That representation preserves every digit when the fundamental solution is unexpectedly large, which happens for some fairly modest values of D. You can therefore copy the answer into a computer algebra system or arbitrary-precision library without rounding it first.

How continued fractions find the answer

The algorithm expands √D as a periodic simple continued fraction. Its convergents provide increasingly accurate rational approximations p/q to √D, and the numerator and denominator of one convergent eventually satisfy p² − Dq² = 1. The solver updates the continued-fraction state and both convergent recurrences with deterministic integer operations. After each step it tests the Pell identity using BigInt arithmetic, returning immediately at the first positive solution. That first successful convergent is the fundamental solution, so the tool does not need to search arbitrary pairs of integers or guess a range for x and y. The reported iteration count is the number of convergents tested, including the initial integer part of √D. This field can help students compare different period lengths or help developers inspect performance, but it does not change the mathematical result. No numerical approximation is used in the final identity test. The ordinary square root operation is used only to obtain the exact integer floor for the bounded D input, while every potentially large solution value and every equality check remains exact.

Using and checking the returned values

Send an object with the field d, such as {"d": 13}. The uppercase alias D is also accepted for formulas copied from conventional notation. A successful response includes d, x, y, and iterations. Treat x and y as decimal integer strings in application code: languages with arbitrary-precision integers can parse them directly, while systems limited to fixed-width numbers should retain the strings or use a big-integer package. To verify an answer independently, square x, subtract D times the square of y, and confirm that the result is exactly one. Do that verification with integer arithmetic, not floating point. Invalid types, fractions, values outside the published range, and perfect squares produce an invalid-input error instead of a misleading result. The solver has no network calls, randomness, stored state, or dependence on the current date, so the same D always yields the same response. Browser use is convenient for individual calculations, while the API price is $0.002 per request when you need repeatable integration in a script, worksheet backend, teaching service, or test suite.

Check number theory homework

Compute the canonical smallest solution, then verify a hand-derived continued-fraction expansion against exact x and y values.

Build deterministic test fixtures

Generate exact Pell solutions for validating arbitrary-precision arithmetic, recurrence code, or computer algebra routines.

Explore continued-fraction periods

Compare the returned iteration counts and solution sizes for different nonsquare values of D.

What does the solver return?

It returns the input d, the fundamental positive solution x and y as exact decimal strings, and the number of convergents tested.

Why are x and y strings?

Pell solutions can exceed the safe integer range of common JSON runtimes. Decimal strings preserve every digit without rounding.

Why is a perfect-square D rejected?

For square D, the equation has no nontrivial solution with positive y, so a fundamental positive Pell solution does not exist.

What input range is supported?

D must be a whole nonsquare integer from 2 through 10,000,000.

How much does an API call cost?

Each API request costs $0.002. The capability can also run free in the browser.

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.

POSThttps://api.kit.forhosting.com/numth/pell-equation-solver

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.

curl -X POST https://api.kit.forhosting.com/numth/pell-equation-solver \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"d":61}'
{
  "d": 61
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.pell_equation_solver",
  "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.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_d10000000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Read the full KIT documentation →