Happy number checker
19 is happy: 1² + 9² gives 82, then 68, then 100, then 1, in four steps. Enter any positive integer to see whether it reaches 1 or falls into the endless unhappy loop, with every intermediate value listed. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 19
- Iterations
- 4
- Sequence
- 19, 82, 68, 100, 1
- Terminal
- 1
A happy number is a positive integer that eventually reaches one when you repeatedly replace it with the sum of the squares of its decimal digits. If the process enters a cycle that never hits one, the number is unhappy (sometimes called sad). The walk is short, fully deterministic, and famous in recreational number theory and coding interviews. This checker takes a single positive integer, runs the digit-square map with cycle detection, and returns whether the number is happy together with the full sequence of intermediate values so you can audit every step. Values below one are rejected as invalid input.
What a happy number is and how the digit-square process works
Begin with any positive integer written in base ten. Square each of its digits, add those squares, and treat the total as the next value in the sequence. Repeat. If you ever land on one, the original number is happy.
If you revisit a number already seen, you have entered a finite cycle that does not contain one, so the original number is unhappy. A textbook happy example is nineteen: one squared plus nine squared is eighty-two; eight squared plus two squared is sixty-eight; six squared plus eight squared is one hundred; one squared plus zero squared plus zero squared is one.
An unhappy starter such as two quickly reaches four, then sixteen, thirty-seven, fifty-eight, eighty-nine, one hundred forty-five, forty-two, twenty, and back to four, which is the classic cycle shared by all unhappy positives under this map. The definition is purely arithmetic: no probability, no external tables, and no dependence on floating-point approximation when you stay inside ordinary integers.
Teaching materials often contrast happy numbers with related digit processes such as digital roots or additive persistence; those reduce by summing digits once, whereas happiness squares each digit before summing and cares about reaching one versus looping. Because every non-negative integer maps to a strictly smaller value once it has more than a few digits, or at least into a small closed set of attractors, the algorithm always terminates after a modest number of iterations.
That makes the property ideal for automated checks, contest problems, and classroom demos where students must show the path rather than only a yes-or-no label.
What you can do with it
Interview and contest problem checks
Candidates and graders can verify happy-number classification for arbitrary positives and inspect the full digit-square sequence without writing a local loop.
Classroom number-theory demos
Teachers can show how summing squared digits leads to one or to the classic unhappy cycle, with every intermediate value visible for discussion.
FAQ
What does the sequence field contain?
Sequence lists every value from the original n through each sum of squared digits until the walk reaches 1 or re-enters a previously seen cycle member.
Is the happy number checker free?
The browser tool runs free on your device.
Are results deterministic?
There is no model, network call, or randomness in the path.
Is 19 a happy number?
Yes. Squaring and adding its digits gives 82, then 68, then 100, and finally 1, so 19 reaches 1 after four steps. The checker lists that whole path, not only the yes-or-no answer.
What happens to an unhappy number?
It never reaches 1 and instead falls into a repeating loop. Starting from 2, for example, you get 4, 16, 37, 58, 89, 145, 42, 20 and back to 4, the cycle every unhappy number ends in.
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/happy-number \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":19}'const res = await fetch("https://api.kit.forhosting.com/numth/happy-number", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 19
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/happy-number",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 19
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/happy-number", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":19}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":19}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/happy-number", 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": 19
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.happy_number",
"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
How this checker evaluates happiness and reports the sequence
The implementation validates that n is present, finite, integral, and at least one. It then walks the sum-of-squared-digits map while recording every value in order. A set of previously seen values detects cycles the moment a repeat appears, so the loop cannot run forever. Call the capability with a field n holding a positive integer, either as a JSON number within the safe range or as a plain digit string that normalizes to an integer. When the walk stops, the engine reports is_happy true if the terminal value is one and false otherwise, iterations as the count of digit-square steps applied, the full sequence including the terminal entry, and terminal itself for a compact machine-readable stop condition. Missing n, non-integers, values below one, and integers above the published maximum fail with invalid_input and are not charged.
Practical use, limits, pricing, and delivery channels
Use the browser tool when you want an immediate happy-number verdict while studying LeetCode-style problems, preparing a number-theory lesson, or checking a contest answer without installing a computer-algebra system. Accepted inputs are positive integers up to the published maximum so classroom examples and interview demos stay exact inside ordinary number ranges. Results never depend on clocks, random seeds, or remote arithmetic services. Each successful request is one billable item at $0.002; failed validation does not consume balance. Wire the endpoint into coding-interview practice apps, recreational math pages, integer classification pipelines, or teaching dashboards where you already prefer exact digit arithmetic over opaque black-box calculators. Delivery works from the web form, the task API, SDK clients, and other KIT channels without changing the mathematical contract.
Integration use cases
Automated integer classification pipelines
Scripts that tag integers as happy or unhappy can call a single deterministic item-priced endpoint instead of embedding ad hoc cycle detection.
Technical questions
How do I check a happy number with this API?
Send a JSON body with positive integer field n to the numth.happy_number capability. The response returns is_happy, iterations, the ordered sequence, and the terminal value.
What happens if n is below 1?
The request fails with invalid_input. This checker requires a positive integer; zero and negatives are rejected and not charged.