ForHosting KIT · Math

Thue-Morse sequence bit

Position 7 is 111 in binary, three ones, so its Thue-Morse bit is 1. Enter any non-negative position to see its binary form, how many ones it has and the resulting 0 or 1. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
N
7
You get
Binary
111
Ones count
3
Thue morse bit
1

The Thue-Morse sequence is the classic overlap-free binary sequence built by taking each non-negative integer n and writing the parity of how many ones appear in its binary expansion. Index zero is zero (binary zero has no ones), index one is one, index two is one, index three is zero, and so on, producing the familiar block 0, 1, 1, 0, 1, 0, 0, 1. Students of combinatorics on words, coding-theory courses, and automatic-sequence research need a fast, deterministic answer for an arbitrary safe integer index without rolling their own bit counters.

What the Thue-Morse bit is and why popcount mod 2 defines it

The infinite Thue-Morse sequence t(0), t(1), t(2), … is defined by t(n) equal to the sum of the binary digits of n taken modulo two. Equivalently, t(n) is zero when n has an even number of one-bits and one when that count is odd. That definition is exactly the population-count (popcount) of n reduced modulo two, so no recursive morphism or paper-folding construction is required at runtime: convert n to base two, count the ones, and take parity.

The first sixteen terms illustrate the pattern: for n from zero through fifteen the bits are 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0. The sequence is cube-free and overlap-free, which is why it appears in combinatorics on words, in constructions of automatic sequences, and in simple demos of aperiodic binary strings. Because the map depends only on the binary ones of a single non-negative integer, the answer is unique, side-effect free, and identical across every language that implements integer popcount the same way.

Negative indices are outside the standard definition used here: the sequence is indexed by the non-negative integers, so a negative n is rejected rather than folded into two's-complement bit patterns that would confuse classroom expectations.

LMS and exam auto-grading

Mark Thue-Morse sequence items with a deterministic oracle that rejects negative n and never invents bits from two's-complement patterns.

Notebooks and aperiodic bit demos

Sample t(n) in coding-theory or combinatorics notebooks without importing a full computer-algebra library for a one-bit parity check.

How is the n-th Thue-Morse bit computed?

t(n) is the parity of the number of ones in the binary form of n: count the 1-bits (popcount) and reduce modulo 2.

What happens if n is negative?

The sequence is indexed by non-negative integers; negative n is rejected and is not billed.

Is this the same as a parity bit on a bit string?

Related idea, different input. Here the input is the integer index n; the bit is popcount(n) mod 2. A generic parity-bit tool works on an arbitrary bit string you supply.

What is the Thue-Morse bit at position 7?

It is 1. Seven written in binary is 111, which contains three ones. Three is odd, and an odd number of ones always gives 1, while an even number gives 0.

What are the first terms of the Thue-Morse sequence?

Starting at position zero, the sequence opens with 0, 1, 1, 0, 1, 0, 0, 1 and continues 1, 0, 0, 1, 0, 1, 1, 0. Each block is the previous one followed by its flipped copy.

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.

POSThttps://api.kit.forhosting.com/numth/thue-morse

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.

curl -X POST https://api.kit.forhosting.com/numth/thue-morse \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"n":7}'
{
  "n": 7
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.thue_morse",
  "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.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

This capability accepts n, returns the binary string, the ones count, and the single Thue-Morse bit t(n), and rejects negative n with invalid_input so automated graders never treat a negative index as a valid sequence position.

What the Thue-Morse bit is and why popcount mod 2 defines it

When you need the bit for a concrete homework value—say n equals seven, binary one-one-one, three ones, parity one—this endpoint returns that bit together with the intermediate binary string and ones count so learners can audit the parity step by step.

Input contract, response fields, and error behaviour

Negative n fails with a clear message that n must be non-negative; those validation failures are not billed. POST a JSON body with a single required field n: a non-negative integer, either as a JSON number or as a decimal digit string that fits JavaScript safe integers. Missing n, empty strings, non-integers, non-finite numbers, and values outside the safe integer range fail with invalid_input naming the problem. On success the body echoes n, provides binary as the base-two string without a 0b prefix (zero yields the single character 0), ones_count as the number of one-bits, thue_morse_bit as 0 or 1, and a short formula string restating t(n) equals popcount(n) mod 2. Example: n equals seven yields binary 111, ones_count three, and thue_morse_bit one. Example: n equals zero yields binary 0, ones_count zero, and thue_morse_bit zero. Example: n equals three yields binary 11, ones_count two, and thue_morse_bit zero. Clients that only need the bit can ignore the diagnostic fields; tutors and graders can display binary and ones_count next to the answer so students see why the parity flipped.

Where a Thue-Morse bit API earns its keep

Coding-theory and digital-signal demos sometimes sample Thue-Morse bits as a deterministic aperiodic binary source for illustrations of discrepancy or paper-folding analogies. Combinatorics and discrete-math courses use the Thue-Morse sequence when teaching automatic sequences, morphisms, and cube-free words; a tiny API lets LMS plugins check student answers for arbitrary n without shipping a full symbolic package. Research notebooks that need a quick oracle for t(n) while exploring paperfolding constants or Prouhet–Thue–Morse related sums can call this endpoint instead of pasting ad-hoc bit tricks that diverge on zero and on large safe integers. Because every successful call costs the published $0.002 base price with no model tokens and no outbound network, failed validation does not consume balance, and the algorithm is a handful of bit operations, you get reproducible bits for homework, grading, and edge workers without standing up heavier number-theory infrastructure. Keep requests small, document that n is a non-negative safe integer, and treat thue_morse_bit as the canonical answer field for automated checks.

Classroom and homework checks

Let students submit a non-negative index n and verify the Thue-Morse bit against popcount parity, with binary and ones_count shown for partial credit.

What is t(0) in this calculator?

Zero. Binary 0 has zero ones (even parity), so thue_morse_bit is 0, with binary "0" and ones_count 0.

What does the Thue-Morse sequence calculator cost?

The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.

Read the full KIT documentation →