Smallest prime factor calculator
Finds the least prime that divides your number, plus what is left after dividing: for 84 the smallest prime factor is 2 and the cofactor is 42. If the factor equals the number, it is prime. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 84
- Smallest prime factor
- 2
- Cofactor
- 42
The smallest prime factor of an integer is the least prime that divides it exactly. For a prime number that factor is the number itself; for a composite it is the first prime that appears in its factorization and the cheapest certificate that the value is not prime.
What the smallest prime factor is and what this tool returns
Every integer n greater than one has a unique multiset of prime factors by the fundamental theorem of arithmetic, and among those primes there is always a least one. That value is called the smallest prime factor, often abbreviated SPF in number-theory tables and programming contest problems. When n is prime the SPF is n; when n is even the SPF is always two; when n is a product of larger primes the SPF is the first prime that divides n under ordered trial.
This capability answers only that question: it does not list every prime power, does not compute Euler’s totient, and does not claim primality certificates for multi-thousand-bit integers.
How the deterministic trial algorithm works
The engine checks divisibility by two and three first, because even and triple multiples dominate everyday inputs and resolve in constant time. Remaining candidates walk a six-step wheel of the form 6k±1 starting at five, which skips every integer divisible by two or three and still hits every prime. As soon as a candidate divides n, that candidate is returned as the smallest prime factor—no further search is needed because candidates are tested in increasing order.
If the loop reaches the integer square root of n without finding a divisor, n itself must be prime and is returned as its own least prime factor. All arithmetic uses BigInt internally within the published safe-integer range so intermediate products stay exact, then results are serialized as ordinary JavaScript numbers for the response.
Where a least-prime-factor calculator fits real workflows
Students and teachers use a smallest prime factor calculator to verify homework, to show why a number fails a primality claim by naming the first obstructing prime, and to build intuition before learning complete factorization. Application developers call it when a pipeline only needs the least factor—for example to peel evenness, to pollard-style trial before heavier methods, or to reject obviously composite configuration values with a concrete prime rather than a bare false.
Data engineers gate numeric columns that should be prime-shaped: any value whose SPF is smaller than the value itself is composite, and the factor lands in the audit log without expanding the full factor list. Contest solvers and educational APIs expose SPF as a first building block toward omega counts, radical computations, and sieve tables.
Prefer this tool when the search intent is strictly “what is the least prime factor of n?”—not when you need every prime power, primality certificates for huge multi-precision integers, or probabilistic tests with custom witness lists.
What you can do with it
Homework and teaching demos
Show the least prime that divides a student answer so composite mistakes are explained by a concrete prime rather than a bare “wrong.”
Quick composite certificate in pipelines
Reject or flag configuration values and IDs that must look prime by returning the first obstructing prime factor for the audit log.
Pre-check before heavier factorization
Peel the smallest prime factor first in scripts that later call full factorization only when the cofactor is still large.
FAQ
What does it cost?
The same calculation also runs free in your browser on this page.
Which numbers are accepted?
Whole integers greater than or equal to 2, up to the published safe-integer maximum.
What is the smallest prime factor of a prime number?
The number itself. When no smaller prime divides it, the prime is its own least factor, the cofactor is 1 and the result is marked as prime.
Is the smallest prime factor of an even number always 2?
Yes. Every even number is divisible by 2, and 2 is the smallest prime, so the search stops there at once and the cofactor is simply half the number.
How does the search work?
It tries 2 and 3 first, then only candidates of the form 6k±1 starting at 5, in increasing order. The first one that divides the number is the answer; past the square root, the number is prime.
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/smallest-prime-factor \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":84}'const res = await fetch("https://api.kit.forhosting.com/numth/smallest-prime-factor", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 84
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/smallest-prime-factor",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 84
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/smallest-prime-factor", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":84}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":84}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/smallest-prime-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": 84
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.smallest_prime_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.
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
This calculator takes a single whole number greater than or equal to two, runs deterministic trial division with a six-step wheel, and returns the least prime factor together with the cofactor and an is_prime flag—so homework checks, coding challenges, and data audits get one clear answer without requesting a full prime-power expansion. The free browser path matches the API byte for byte at $0.002 per successful request.
What the smallest prime factor is and what this tool returns
A successful response echoes the original n as a number, reports smallest_prime_factor as that least prime, sets is_prime true exactly when the factor equals n, and includes cofactor equal to n divided by the factor so you can see the complementary quotient in one call. Input that is missing, fractional, non-numeric, boolean, or strictly less than two is rejected with invalid_input before any arithmetic runs, so a float such as 9.5 or a zero never produces a misleading factor.
How the deterministic trial algorithm works
Worst-case work is proportional to the square root of n for primes and semi-primes with large factors, which is acceptable for classroom values and safe-integer composites; for full factorization of huge integers you would use a dedicated factorization capability instead of this least-factor endpoint.
Where a least-prime-factor calculator fits real workflows
Because the capability is tier A, you can paste a number on this page and run the identical logic free in the browser; automate the same contract later at $0.002 per request when you wire POST-style tasks into scripts or SDKs. Keep n an integer of at least two within the published safe range, treat invalid_input as a client fix rather than a retry, and use the cofactor field when you want the complementary quotient without a second division on the client.
Technical questions
What is returned when n is prime?
smallest_prime_factor equals n, is_prime is true, and cofactor is 1.
How is this different from full prime factorization?
This endpoint returns only the least prime factor and its cofactor. It does not list every prime power or compute omega counts.
Is the algorithm randomized?
No. Trial division is fully deterministic: the same n always produces the same JSON with no random witnesses or wall-clock seeds.