Integer compositions calculator
Count every ordered way to split a whole number into positive parts: 5 has 16 compositions and 4 has 8, because 1+3 and 3+1 count separately. Ask for exactly k parts to narrow the count. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Positive integer to compose
- 5
- Quantity
- 16
An integer composition of n is an ordered way to write n as a sum of positive integers. Unlike partitions, order matters: 1+3 and 3+1 are different compositions of four. Classic combinatorics proves there are exactly two to the power of n minus one compositions of any positive n, and exactly C(n−1, k−1) compositions that use precisely k parts. This capability evaluates those formulas with BigInt arithmetic so large counts stay exact as decimal strings instead of overflowing floating-point or safe-integer limits.
What compositions are and how the two formulas work
Place n ones in a row and decide, for each of the n−1 gaps between them, whether to insert a separator. Every pattern of separators yields a unique ordered sum of positive parts that totals n, and every composition arises this way—so the total number of compositions is two raised to n minus one. When you require exactly k parts, you must choose exactly k−1 of those n−1 gaps to place separators, which is the binomial coefficient C(n−1, k−1).
For a small check: n equals four has compositions (4), (1,3), (3,1), (2,2), (1,1,2), (1,2,1), (2,1,1), and (1,1,1,1)—eight values, matching two to the three. Exactly three parts of five: C(4,2) equals six. This tool multiplies and divides with JavaScript BigInt so every intermediate stays exact and the final count is returned as a decimal string, never rounded.
What you can do with it
Programming contest and interview prep validators
Assert candidate solutions for composition-counting problems without reimplementing BigInt powers or binomials in every language harness.
Teaching stars-and-bars and ordered sums
Demonstrate that compositions of n equal 2^(n-1) and exactly-k-parts equal C(n-1,k-1) with live exact counts while introducing generating functions or recursion.
FAQ
What is an integer composition?
A composition of n is an ordered tuple of positive integers that sum to n. Order matters: (1,3) and (3,1) are different compositions of 4.
How many compositions does a positive integer n have?
Exactly 2^(n−1) compositions in total. Compositions into exactly k parts number C(n−1, k−1).
Why is the count returned as a string?
Large powers of two and binomials exceed JavaScript safe integers. A decimal string keeps the exact value for every supported n without precision loss.
Is the integer compositions calculator free?
The browser tool is free and runs locally.
How many compositions of 5 have exactly 3 parts?
Six. Enter 5 as the number and 3 as the number of parts; the count is C(4,2), because you choose 2 of the 4 gaps between five ones for the plus signs.
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/combo/compositions \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":5}'const res = await fetch("https://api.kit.forhosting.com/combo/compositions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 5
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/combo/compositions",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 5
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/combo/compositions", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":5}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":5}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/combo/compositions", 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": 5
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "combo.compositions",
"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 | 10000 |
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
Students, teachers, contest programmers, and data engineers get a deterministic JSON result—no computer-algebra install, no random sampling, and the same pure function in the free browser tool and the prepaid API path—so homework checks, pipeline validation, and interview sandboxes all share one reproducible answer whenever they need composition counts rather than unordered partitions or binomial n-choose-r alone.
What compositions are and how the two formulas work
Boundary cases are intentional: n must be at least one (zero and negatives are invalid_input, not a free extension), k when present must satisfy one through n, and omitting k returns the total. That contract matches standard combinatorics textbooks and programming-contest judges, so you can trust the number against hand listings for small n and against known tables for larger ones without floating-point drift or silent wraparound.
How to call the API and read the response
When k is omitted or empty, the service returns the total composition count two to the power n minus one. When k is set, it returns C(n−1, k−1) and requires one less than or equal to k less than or equal to n. A successful response includes n as a number, k as a number or null when the total mode was used, count as a decimal string of the exact value, and formula as either the string 2^(n-1) or C(n-1,k-1) so clients can display or log the identity without hard-coding it. Use it from homework graders, programming-contest validators, interview-prep sandboxes, generating-function demos, or any workflow that needs a reproducible composition count without installing multiprecision packages on every machine that runs the pipeline. Send a JSON body with required positive integer n—the integer you want to compose—from one through ten thousand. Aliases total, N, and integer are accepted for the same meaning. Optional field k is the exact number of parts; aliases parts, num_parts, and K map to k. Failed validation—missing n, n below one, a non-integer, a fraction, a value above the published maximum, or k greater than n—returns invalid_input and is never charged. Pricing is a flat $0.002 per successful request with unit item and zero marginal unit price.
Practical tips, limits, and related intents
When teaching, prefer paired examples: n equals four yields eight total compositions; n equals five and k equals three yields six; n equals five and k equals one yields one (the single part equal to n); n equals five and k equals five yields one (all ones); n equals zero demonstrates the hard rejection path for n below one; and k greater than n demonstrates the k-out-of-range path. Sibling landings may target combinations (n choose r), multinomials, or unordered partitions; this page is specifically for people searching for an integer compositions calculator, ordered partitions into positive parts, or compositions of n into k parts. Do not confuse compositions with partitions: if order does not matter, you need the partition function p(n), which is a different intent and a different landing. Keep n at most ten thousand so worst-case powers and binomials finish quickly on the Worker and in the browser while still covering contest and teaching sizes far beyond safe Number range. If you need multiprecision work with n in the hundreds of thousands, use a dedicated big-integer stack offline—this lightweight parse capability is built for interactive checks, API glue, and classroom demos. Because both formulas are deterministic, students can recompute small cases by listing or with a spreadsheet and match the API line by line. Failed tasks that return invalid_input do not consume prepaid balance, which keeps exploratory scripts cheap when users type bad values while learning the contract. Prefer exact string counts over forcing a JavaScript Number so large powers of two never lose low-order digits in JSON transport.
Integration use cases
Homework and exam grading for compositions
Give students n (and optional k) and check their hand-counted compositions against a deterministic API or free browser result without floating-point ambiguity.
Technical questions
What happens if n is below 1 or k is greater than n?
The request fails with invalid_input. This calculator requires n ≥ 1 and, when k is set, 1 ≤ k ≤ n.