ForHosting KIT · Math

Sum of two squares checker

65 is a sum of two squares, 1² + 8², while 3 and 7 never are. Enter a whole number to get a yes or no, one working pair and the prime factors behind Fermat's rule. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon

A non-negative integer is a sum of two integer squares when there exist integers a and b such that n equals a squared plus b squared. Zero is 0² + 0², five is 1² + 2², thirteen is 2² + 3², and sixty-five is 1² + 8² (or 4² + 7²); three and seven cannot be written that way. Fermat’s classical theorem decides membership from the prime factorization alone: n works if and only if every prime congruent to three modulo four appears with an even exponent. The arithmetic is fully deterministic—no network, no randomness, no clock—so homework checks, contest oracles, and production pipelines always agree.

What “sum of two squares” means and how Fermat decides it

Integers a and b form a representation of n when a squared plus b squared equals n. Signs and order do not change the underlying identity: if (a, b) works then so do (±a, ±b) and (±b, ±a), so this checker reports one non-negative pair with a less than or equal to b when a representation exists. Zero and one succeed immediately (0²+0² and 0²+1²).

Two is 1²+1²; every prime congruent to one modulo four is itself a sum of two squares; primes congruent to three modulo four never are. The full theorem multiplies these facts: write n as a product of prime powers; if any prime p ≡ 3 (mod 4) has an odd exponent, n is blocked and cannot be a sum of two squares; if every such prime has even exponent (including the empty case when none appear), n can.

Equivalently, after removing all square factors and all factors of two and of primes ≡ 1 mod 4, nothing of the form 4k+3 may remain to an odd power.

Where sum-of-two-squares checks fit in study, contests, and automation

Students meet sums of two squares when learning modular arithmetic, quadratic residues, and early algebraic number theory: the obstruction primes ≡ 3 mod 4 are a first bridge between factorization and Diophantine representation. Teachers can generate answer keys for worksheets that ask whether five, thirteen, twenty-five, or sixty-five is a sum of two squares, and can contrast them with three, seven, or eleven without trusting opaque calculator apps. Contest practice and CTF write-ups often need a fast offline oracle for “can n be written as a²+b²?”

so solutions can be checked without hard-coding OEIS slices. Library authors use the same contract to property-test factorization helpers and Gaussian-integer norms. Keep n within the published limit so trial division and the optional representation search stay interactive; for cryptographic-size integers you need multiprecision factoring, not this lightweight parse capability.

Contest and CTF two-square checks

Verify sum-of-two-squares membership quickly while solving programming contest or capture-the-flag problems without embedding static tables of representable integers.

Teaching Fermat’s 4k+3 criterion

Demonstrate that primes ≡ 1 mod 4 and products like 65 succeed while primes ≡ 3 mod 4 with odd exponent (3, 7, 11, 21) fail, with factors and blocking primes for the board.

When is n a sum of two squares?

A non-negative integer n equals a² + b² for some integers a, b if and only if every prime congruent to 3 modulo 4 has even exponent in the prime factorization of n. Zero, one, two, five, thirteen, and sixty-five succeed; three and seven fail.

What happens if n is negative?

The classical sum-of-two-squares theorem is stated for non-negative integers; negative n is out of domain and is not billed.

What does the sum of two squares checker cost?

The browser tool is free and runs locally.

Is the algorithm randomized?

No. It uses deterministic trial-division factorization, exact parity checks on primes ≡ 3 mod 4, and an exact search for one representation a ≤ b when membership holds. The same n always yields the same flag and factors.

Is 65 a sum of two squares?

Yes: 65 equals 1² + 8², and also 4² + 7². Its prime factors are 5 and 13, and both leave a remainder of 1 when divided by 4, so nothing blocks the representation.

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/sum-two-squares-check

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/sum-two-squares-check \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"n":65}'
{
  "n": 65
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.sum_two_squares_check",
  "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.

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.

This capability factors n, lists any blocking primes of the form 4k+3 with odd multiplicity, returns a clear is_sum_of_two_squares flag, and when the answer is yes supplies one non-negative representation a ≤ b. Explore free in your browser, or call the API at $0.002 per successful request when scripts need a reproducible yes-or-no verdict without a computer-algebra stack.

What “sum of two squares” means and how Fermat decides it

This tool never samples or approximates: it factors n once with trial division, scans each prime power for the 4k+3 odd-exponent condition, fills blocking_primes when the answer is no, and only then searches for a concrete pair a²+b² when the criterion says yes, so textbook membership and the boolean flag always match.

Input contract, response shape, and error behaviour

Negative n is explicitly out of domain: the classical statement is for non-negative integers, and the capability surfaces that with a clear message rather than inventing a false success. Send a JSON body with a single required field n—a whole number at least zero and at most one trillion (ten to the twelfth). Aliases number and value are accepted for the same field so forms and query strings can reuse one contract. On success the response includes n as accepted, is_sum_of_two_squares as a boolean, factors as an ordered list of objects each with prime and exponent (empty for zero and one), blocking_primes as the ascending list of primes ≡ 3 mod 4 with odd exponent, criterion as a short restatement of Fermat’s rule, and when the boolean is true a representation object with non-negative integers a and b such that a² + b² equals n and a ≤ b. Failed validation—missing n, a non-integer, a fraction, a negative value, or a value above the published limit—returns invalid_input and is never charged. Pricing is a flat $0.002 per successful request (unit item with zero marginal unit price). Integers that fail the criterion still return a successful response with is_sum_of_two_squares false and a non-empty blocking_primes list; only domain violations are errors, which keeps grading scripts simple when students submit three, seven, or twenty-one.

Where sum-of-two-squares checks fit in study, contests, and automation

On this page the work runs entirely in your browser; through the API each successful lookup costs $0.002, failed validation is not charged, and nothing is stored after the response returns. Sibling landings for sum of the first n squares or happy numbers (digit square sums) answer different search intents—this page targets people who specifically want Fermat’s two-square membership test and a clear is_sum_of_two_squares flag.

Homework: can n be written as a² + b²?

Give students an integer and check their hand factorization and yes/no membership against a deterministic API or free browser result with blocking primes listed.

Does a false answer still return factors?

Yes. Non-representable integers succeed with is_sum_of_two_squares false, the full factor list, and blocking_primes naming every prime ≡ 3 mod 4 with odd exponent. Only domain violations are errors.

Read the full KIT documentation →