p-adic valuation calculator
Count how many times a prime divides a whole number: 72 is 8 times 9, so it contains the prime 2 three times, its 2-adic valuation is 3 and the leftover part is 9. Negative numbers work too. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 72
- Power (W)
- 2
- Valuation
- 3
- Residual
- 9
The p-adic valuation is the number-theoretic exponent that measures how many times a prime p divides an integer n. Algebra courses, algebraic number theory, contest problems on trailing factors, and p-adic analysis all use this single integer. Students often confuse it with the full prime factorization or with the Legendre formula for factorials. This calculator accepts non-zero n and prime p, verifies primality, divides out powers of p from the absolute value of n, and returns the valuation together with the p-free residual and a short formula string. Composite or non-prime bases are rejected so the classical definition stays intact.
What the p-adic valuation measures
Equivalently, write the absolute value of n in the form p to the k times m where m is an integer not divisible by p; then the valuation is k and m carries the sign of n in this calculator as the residual. When p does not divide n at all, the largest such k is zero and the residual equals n. When n is a pure power of p, the residual has absolute value one.
The definition deliberately excludes n equal to zero. Number theory textbooks introduce it before local fields, while programming contest material uses it when counting factors of two or five in a product, simplifying fractions modulo a prime power, or testing square-free parts after stripping prime powers. Returning both valuation and residual lets callers reconstruct n as p to the valuation times residual and verify the identity without a second factorization pass.
What you enter and what you get back
Even composites, squares of primes, and any non-prime integer fail with a clear message that p must be prime; those failures are not billed. Zero for n is rejected with a message that the p-adic valuation of zero is undefined. Example: n equals seventy-two and p equals two yields valuation three and residual nine because seventy-two equals eight times nine.
Example: n equals forty-five and p equals two yields valuation zero and residual forty-five because two does not divide forty-five. The algorithm only divides by p repeatedly; it never factors n completely, so large highly composite n with small prime p stay fast.
Where the p-adic valuation is useful
Classrooms and tutoring platforms use a p-adic valuation calculator so learners can check the exponent of a chosen prime without writing a full factorization loop for every homework item. Contest programming helpers need a deterministic oracle that strips powers of p when reducing fractions, computing multiplicity in products, or verifying square-free kernels after removing p-powers. Failed validation and non-prime p do not consume balance.
Keep requests inside the published bounds, pin valuation and residual in tests, and you get reproducible exponents for homework, LMS plugins, and edge workers without standing up symbolic math infrastructure of your own. Sibling number-theory landings in this kit cover full prime factorization, factorial exponents via Legendre, and trailing zeros; this page specifically answers people who search for the valuation of one integer at one prime.
What you can do with it
p-adic and local-field notebooks
Compute finite valuations on integers before lifting examples to Z_p or Q_p without embedding a computer-algebra library.
FAQ
What happens if n is zero?
The p-adic valuation of zero is conventionally +∞, not a finite integer, so zero is rejected and not billed.
What happens if p is not prime?
Composite, unit, and negative bases are rejected; failed validation is not charged.
Does the valuation depend on the sign of n?
The residual keeps the sign of n so that n equals p^valuation times residual still holds.
What is the 2-adic valuation of 72?
It is 3, with a leftover part of 9. The prime 2 divides 72 three times because 72 is 8 times 9, and 9 can no longer be divided by 2.
What does the leftover part mean?
It is what remains after removing every factor of the prime, so it is not divisible by that prime. Multiplying it back by the prime raised to the valuation gives your number; 45 with the prime 2 gives valuation 0 and leftover 45.
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/p-adic-valuation \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":72,"p":2}'const res = await fetch("https://api.kit.forhosting.com/numth/p-adic-valuation", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 72,
"p": 2
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/p-adic-valuation",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 72,
"p": 2
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/p-adic-valuation", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":72,"p":2}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":72,"p":2}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/p-adic-valuation", 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": 72,
"p": 2
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.p_adic_valuation",
"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
Write n as p to the power k times a unit not divisible by p; that k is v_p(n). Zero is rejected because v_p(0) is conventionally plus infinity, not a finite integer.
What the p-adic valuation means and how v_p(n) is defined
For a fixed prime p and a non-zero integer n, the p-adic valuation v_p(n) is the largest integer k greater than or equal to zero such that p raised to k still divides n. Negative n receive the same valuation as their absolute value because divisibility ignores sign: v_p of negative twelve at p equals two is the same as for twelve. In the p-adic absolute value, the valuation of zero is taken as plus infinity so that the product formula and ultrametric properties stay clean; this API therefore fails with invalid_input rather than inventing a finite code for infinity. The valuation is additive on products: v_p of a times b equals v_p of a plus v_p of b, and it is the building block for the p-adic absolute value given by p to the power of minus the valuation.
Input contract, algorithm, response shape, and errors
POST a JSON body with two required integers: n, a non-zero integer whose absolute value is at most one trillion, and p, a prime at least two and at most one trillion. Each field may arrive as a JSON number or as a plain digit string; missing fields, fractions, non-finite values, and out-of-range magnitudes fail with invalid_input naming the field. On success the body echoes n and p, returns valuation as a non-negative integer, residual as the signed integer n divided by p to the valuation, divides as a boolean true when valuation is positive, and formula as a short string of the form v_p of n equals k.
Where a p-adic valuation API earns its keep
Algebraic number theory notebooks reduce local questions about Z_p and Q_p to finite valuations on integers before lifting to p-adic series; a tiny parse endpoint avoids importing a computer-algebra system for a two-argument check.
Integration use cases
Classroom and homework checks
Let students submit n and prime p and verify v_p(n) plus the p-free residual against hand methods without a full factorization.
Contest and scripting helpers
Strip powers of a fixed prime from products, simplify fractions, or test multiplicity with a deterministic item-priced endpoint.
Technical questions
How do I compute the p-adic valuation with this API?
Send JSON with non-zero integer n and prime p. On success the response returns valuation (the exponent), residual (n / p^valuation), divides, and a short formula string.
What does the p-adic valuation calculator cost?
The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input and non-prime p are not billed.