Balanced ternary converter
20 in balanced ternary is 1 −1 1 −1: three cubed, minus three squared, plus three, minus one. Enter any integer, negative ones included, to get its signed-digit form with clear spacing between the digits. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 20
- Balanced ternary
- 1 -1 1 -1
- Digits
- 1, -1, 1, -1
The balanced ternary converter rewrites an integer in base three while allowing each digit to be minus one, zero, or one. Enter a positive, negative, or zero integer and receive both a readable space-separated representation and a numeric digit array. The spacing keeps minus-one digits unambiguous when you copy the result into notes, code, or a calculation. The conversion is exact, deterministic, and performed with integer arithmetic, so it is useful for studying signed-digit number systems, checking exercises, and preparing small test vectors without doing repeated division by hand.
What balanced ternary represents
Ordinary ternary uses the digits zero, one, and two, with each position worth a power of three. Balanced ternary replaces two with minus one, giving the digit set -1, 0, and 1. A digit still multiplies the power of three at its position, but negative digits can subtract that place value. For example, the displayed sequence is ordered from the highest power to the units position, just as it is in familiar decimal notation.
Because the token -1 occupies one position, this converter separates digits with spaces instead of compressing them into a string that could be misread. The returned digits array provides the same information in a form programs can consume directly. Every integer has a unique finite balanced ternary expansion when leading zeros are excluded.
Positive and negative inputs therefore need no separate sign character: the signs already live inside the digits themselves. This makes the representation symmetrical around zero and especially convenient when comparing an integer with its negation.
How the conversion algorithm works
The converter repeatedly divides the current integer by three and examines the remainder. A remainder of zero or one is already a permitted balanced digit, so it is recorded directly. A remainder of two is replaced by -1 and a carry of one is added to the next position, since two equals minus one plus one group of three.
JavaScript can produce negative remainders for negative dividends, so the mirror case is handled too: a remainder of -2 becomes 1 and the next quotient is reduced by one. Digits are discovered from the units place upward and reversed at the end for normal reading order. The loop stops only when the adjusted quotient becomes zero.
This procedure uses no approximations, lookup service, random value, or clock. Rejecting those values prevents a plausible-looking but incorrect digit sequence from being returned.
Reading, checking, and using the result
To check a result, start at the rightmost digit with the power three to zero, multiply each digit by its corresponding power of three, and add the signed terms. The sum must reproduce the original integer. The original `n` is repeated so logs and batches remain self-describing.
Zero is the sole special presentation case and returns one zero digit rather than an empty sequence. Use the browser tool for quick exploration, classroom work, or checking a single calculation.
What you can do with it
Check number-system exercises
Convert an assigned decimal integer and compare the signed digits with a hand-worked repeated-division solution.
Explore symmetric representations
Compare positive and negative integers to see how signed digits express negation without a separate sign prefix.
FAQ
What digits does balanced ternary use?
It uses -1, 0, and 1. Each position has a place value that is a power of three.
Why are the digits separated by spaces?
Spacing makes -1 a clear single digit and prevents its minus sign from being confused with a sign for the entire number.
Can the converter handle negative integers?
Yes. Negative values are represented directly with signed digits; no separate leading sign is added.
Why are very large integers rejected?
Rejecting them protects the exactness of the result.
How do I check a balanced ternary result by hand?
Start at the rightmost digit, the units place, multiply each digit by its power of three and add the signed terms. For 1 −1 1 −1 that is three cubed minus three squared plus three minus one, which gives 20.
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/balanced-ternary \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":20}'const res = await fetch("https://api.kit.forhosting.com/numth/balanced-ternary", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 20
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/balanced-ternary",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 20
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/balanced-ternary", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":20}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":20}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/balanced-ternary", 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": 20
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.balanced_ternary",
"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
How the conversion algorithm works
The input must be a safe integer because larger JSON numbers may already have lost precision before conversion begins.
Reading, checking, and using the result
The `balanced_ternary` field is intended for people: its tokens are separated by single spaces, and each token is exactly -1, 0, or 1. The `digits` field is intended for software and lists those same values as JSON integers from most significant to least significant. Inputs that are decimals, numeric strings, infinities, or integers outside JavaScript's safe range return an `invalid_input` error instead of being rounded or coerced.
Integration use cases
Generate deterministic test vectors
Create exact balanced ternary digit arrays for unit tests of arithmetic, encoding, or educational software.
Technical questions
How much does an API conversion cost?
Each API request costs $0.002. You can also run the converter free in your browser on this page.