Difference of squares factorization
5959 equals 80² − 21², and that split reveals its factors 59 and 101. Enter an odd composite number and get the two squares, the closest factor pair and an identity you can check by hand. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 5,959
- A
- 80
- B
- 21
- Factor low
- 59
- Factor high
- 101
The difference of squares factorization calculator takes a positive odd composite integer and finds integers a and b for which a squared minus b squared equals the original number. It also returns the corresponding nontrivial factors, because a² − b² always equals (a − b)(a + b). The search begins near the square root, so the reported factors are as close together as possible and b is as small as possible. Even numbers, primes, and 1 produce a clear input error instead of the unhelpful trivial factorization 1 × n.
Turn an odd number into two squares
The algebraic identity a² − b² = (a − b)(a + b) connects a subtraction problem with integer factorization. For an odd composite n, every nontrivial factor pair consists of two odd numbers. If those factors are called d and q, with d ≤ q, then a = (q + d) / 2 and b = (q − d) / 2.
Both results are integers because the sum and difference of two odd integers are even. The calculator first finds a divisor near the square root of n, obtains its paired divisor by exact division, and applies those two formulas. For example, 5959 has the factor pair 59 × 101.
Their half-sum is 80 and their half-difference is 21, giving 80² − 21² = 6400 − 441 = 5959. The response includes n, a, b, both factors, and a readable identity. This makes the result useful as both a calculation and a compact proof that can be checked simply by multiplying the factors or subtracting the squares.
Understand nontrivial results and errors
Every positive odd integer has a formal difference-of-squares representation if the factor pair 1 × n is allowed: a would be (n + 1) / 2 and b would be (n − 1) / 2. That representation does not reveal a useful factor, so this capability deliberately excludes it. A successful response requires a split whose two factors are greater than one.
Consequently, an odd prime and the number 1 return an invalid-input error explaining that no nontrivial split exists. An odd perfect square remains valid. Inputs must be positive finite integers no larger than the published bound.
Decimal fractions, negative values, malformed strings, and values beyond that bound are rejected rather than rounded, truncated, or approximated.
What you can do with it
Check an algebra exercise
Verify a claimed representation n = a² − b² and see the factor pair that the identity implies.
Teach the factorization identity
Show how two odd factors become the half-sum and half-difference used for a and b.
Find a balanced factor pair
Return the nontrivial factors closest to √n, which also minimizes the nonnegative value of b.
FAQ
Why are even integers rejected?
This capability has an odd-integer contract. Although some even integers are differences of squares, they are outside this factorization workflow.
Why does a prime return an error?
A prime only has the positive factor pair 1 × n.
Which factorization is returned when several exist?
The algorithm returns the factor pair closest to √n, giving the smallest nonnegative b.
How do I write 5959 as a difference of two squares?
Use its factor pair 59 and 101. Half their sum is 80 and half their difference is 21, so 80² − 21² = 6400 − 441 = 5959.
Does it work for an odd perfect square like 81?
Yes. The closest factor pair of 81 is 9 and 9, which gives a first square of 9² and a second of 0², because 9² − 0² = 81.
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/difference-of-squares-factor \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":5959}'const res = await fetch("https://api.kit.forhosting.com/numth/difference-of-squares-factor", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 5959
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/difference-of-squares-factor",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 5959
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/difference-of-squares-factor", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":5959}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":5959}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/difference-of-squares-factor", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"n": 5959
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.difference_of_squares_factor",
"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.
Limits
max_n | 1000000000000 |
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
Understand nontrivial results and errors
Even integers are also rejected by the product contract, even though some even values divisible by four can be written as differences of squares; this endpoint is specifically designed for odd-integer factorization. For n = 81, the closest factor pair is 9 × 9, which produces a = 9 and b = 0, and 9² − 0² = 81.
Why the search is deterministic
The implementation starts with the greatest odd integer at or below √n and tests odd candidates in descending order. The first exact divisor it encounters is the greatest factor not exceeding the square root, so its paired factor is the smallest factor not below the square root. This produces the closest available nontrivial factor pair and therefore the smallest nonnegative value of b. The loop has a fixed lower endpoint and the declared maximum input bounds its work, so it cannot continue indefinitely. It uses exact safe-integer arithmetic throughout the supported range, and it never calls a network service, consults a prime table, reads the clock, uses random choices, or stores data between requests. Identical input therefore produces identical JSON output.
Technical questions
What does it cost?
The API costs $0.002 per request, and the browser version can run locally on this page.
Can b equal zero?
Yes. When n is an odd composite perfect square, equal nontrivial factors produce b = 0.