Ruler Function Calculator

Powers of two

ruler function value nu2(40)3
largest power of two dividing n8
odd part of n5
factorization form40 = 8 x 5
How this was calculated
  1. The ruler function nu2(n) is the exponent of the largest power of two that divides n - the 2-adic valuation of n.
  2. Divide 40 by two repeatedly: it survives 3 divisions before turning odd, leaving the odd part 5.
  3. So 40 = 2^3 x 5 and nu2(40) = 3.
  4. The name comes from a ruler: the longest mark at each position follows this pattern, with every second mark short, every fourth longer, and so on.

The ruler function nu2(n) answers a simple question: how many times can n be divided by two before it turns odd? For 40 the answer is 3, because 40 = 8 x 5. For 96 it is 5, because 96 = 32 x 3. That count - the exponent of the largest power of two dividing n - is also called the 2-adic valuation of n, written v2(n).

The homely name comes from an ordinary ruler. Mark the positions 1, 2, 3, 4, ... along a ruler divided into halves, quarters, eighths and sixteenths. The longest tick at each position follows exactly this pattern: short at odd positions, longer at positions divisible by 2, longer still at multiples of 4, and so on. Read the tick lengths off and you have written down the ruler function.

This calculator tests a single number, reporting the exponent, the power of two and the odd part left behind, or lists the values up to a limit so the self-similar pattern is visible at a glance.

Worked examples

nu2 of 40

(0 - test a single number · 40)

ruler function value nu2(40)3
largest power of two dividing n8
odd part of n5
factorization form40 = 8 x 5

nu2 of 96

(0 - test a single number · 96)

ruler function value nu2(96)5
largest power of two dividing n32
odd part of n3
factorization form96 = 32 x 3

Ruler function values up to 16

(1 - list up to a limit · 16)

ruler function values up to 160, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4
how many entries are 0 (the odd numbers)8
largest value in the range4
first reached at n16

What the ruler function is

Every whole number factors uniquely as a power of two times an odd number: n = 2^e x m with m odd. The ruler function returns the exponent e. Odd numbers have no factors of two at all, so nu2 of any odd number is 0. Powers of two themselves are the extreme case: nu2(2^k) = k, with odd part 1.

In the language of valuations, nu2 is the 2-adic valuation: it measures how divisible n is by 2 in exactly the way a p-adic valuation measures divisibility by a prime p. It satisfies the valuation laws - nu2 of a product is the sum of the values, and nu2 of a sum is at least the smaller of the two values - which is what makes it useful in proofs about binary arithmetic, factorials and infinite series in the 2-adic numbers.

  • n = 2^nu2(n) x (odd part), uniquely
  • nu2(odd) = 0 and nu2(2^k) = k
  • nu2(a x b) = nu2(a) + nu2(b)
  • Also written v2(n), the 2-adic valuation

How to compute nu2 by hand

Divide by two and count. For 40: 40, 20, 10, 5 - three successful divisions before the odd number 5 appears, so nu2(40) = 3, matching the factorisation 40 = 2^3 x 5. For 96: 96, 48, 24, 12, 6, 3 - five divisions, so nu2(96) = 5 and 96 = 2^5 x 3.

In binary the same count is visible instantly: nu2(n) is the number of trailing zeros in the binary expansion of n. 40 is 101000 in binary, ending in three zeros; 96 is 1100000, ending in five. Programmers meet this as the count-trailing-zeros operation, a single instruction on modern processors, and it sits inside everything from binary GCD algorithms to the scheduling of tournament brackets.

  • 40: three halvings reach 5, so nu2(40) = 3
  • 96: five halvings reach 3, so nu2(96) = 5
  • Binary view: count the trailing zeros
  • Hardware view: the ctz / __builtin_ctz instruction

The pattern in a list

Listed from n = 1, the ruler function runs 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4. Up to 16 the largest value is 4, first reached at n = 16 itself - the first multiple of 2^4. In general the value k first appears at n = 2^k and then repeats every 2^(k + 1) steps.

The frequencies halve each time. Half of all whole numbers are odd, contributing 0. A quarter have nu2 = 1, an eighth have nu2 = 2, and a fraction 1 / 2^(k + 1) have nu2 = k. The pattern is perfectly self-similar: delete the zeros, subtract 1 from everything that remains, and the original sequence reappears - the same fractal nesting you see in the ticks of a ruler, where the pattern between two long marks repeats the whole ruler at a smaller scale.

  • Values to 16: 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4
  • Value k first appears at n = 2^k
  • Density of value k is 1 / 2^(k + 1)
  • Self-similar: strip the zeros, subtract 1, and it repeats

Where nu2 shows up

The most famous appearance is Legendre's formula for the exponent of a prime in a factorial. For the prime 2 it says the exponent of 2 in n! is n minus the number of 1 bits in n, and the proof works by summing the ruler function: nu2(n!) = nu2(1) + nu2(2) + ... + nu2(n). That sum also counts the total number of carries when adding in binary, which is why it controls the 2-adic behaviour of binomial coefficients through Kummer's theorem.

The Tower of Hanoi gives a puzzle-flavoured appearance: in the optimal solution, the disk moved at step n is disk number nu2(n) + 1. The smallest disk moves on every odd step, the next on every step with nu2 = 1, and so on - the ruler function is the schedule of the whole solution. The same halving rhythm structures single-elimination tournament seeding and the binary carry chain inside adders.

  • Legendre: exponent of 2 in n! is the sum of nu2(k)
  • Kummer: carries in binary addition counted by nu2
  • Tower of Hanoi: step n moves disk nu2(n) + 1
  • Tournament brackets and binary counters follow the same rhythm

The ruler function in binary arithmetic

In binary, nu2(n) counts trailing zeros, and that single observation powers a family of algorithms. The binary GCD algorithm replaces division with shifts and subtractions, using trailing-zero counts to strip factors of two from both arguments at every step. The lowbit operation, n AND -n, isolates exactly the power 2^nu2(n) - for 40 it returns 8 - and it is the core of the Fenwick tree (binary indexed tree), whose update and query loops climb and descend by lowbit steps. Data structures over arrays of size 2^k are, quietly, ruler-function machines.

The pattern also schedules the classic single-elimination tournament: in a bracket of 2^k players, the strongest seeds are placed so that the rounds follow the ruler pattern, and the number of the round in which seed position p first plays is governed by nu2. Even the humble act of folding a strip of paper generates the sequence - the crease pattern of regular folds, mountain versus valley, is the ruler function's close relative, the paperfolding sequence.

Mathematically, nu2 extends to the rationals and then to the 2-adic numbers, where it becomes a genuine absolute value in reverse: |x|_2 = 2^-nu2(x). In that world, numbers divisible by high powers of two are small, series converge when their terms grow in nu2, and the ruler function stops being a curiosity and becomes a metric. The calculator's modest question - how many times can this be halved - is the doorway into that world.

  • Binary GCD strips factors of two via nu2
  • lowbit = 2^nu2(n) drives Fenwick trees
  • Tournament brackets follow the ruler pattern
  • 2-adic absolute value: |x|_2 = 2^-nu2(x)

Quick reference and checks

A few values worth memorising anchor the function: nu2 of any odd number is 0; nu2(2) = 1, nu2(4) = 2, nu2(8) = 3, nu2(16) = 4 - the powers of two return their own exponent; nu2(12) = 2 because 12 = 4 x 3; nu2(24) = 3 because 24 = 8 x 3; nu2(40) = 3 and nu2(96) = 5 from this page's examples. If a computed value breaks the pattern - an odd input returning anything but 0, or a power of two returning anything but its exponent - the computation is wrong, and these anchors find the error fastest.

The calculator's two modes serve the two natural questions. Test mode answers 'how divisible by two is this number', reporting the exponent, the power and the odd part - the full factorisation split n = 2^e x m. List mode answers 'what does the pattern look like', and its summary statistics (the count of zeros, the maximum, where the maximum first occurs) are the fingerprints of the self-similar structure. Between them they cover how the ruler function is actually used: pointwise in algorithms, globally in proofs.

  • Anchors: nu2(odd) = 0, nu2(2^k) = k
  • nu2(12) = 2, nu2(24) = 3, nu2(40) = 3, nu2(96) = 5
  • Test mode splits n = 2^e x odd
  • List mode exposes the fractal pattern

FAQs

What is the ruler function?

nu2(n) is the exponent of the largest power of two dividing n. For example nu2(40) = 3 because 40 = 8 x 5.

What is nu2(96)?

5, because 96 = 32 x 3 and 32 = 2^5 is the largest power of two dividing 96.

Why is it called the ruler function?

Its values match the tick marks on a ruler: every second position gets a short mark, every fourth a longer one, every eighth longer still, following exactly the pattern 0, 1, 0, 2, 0, 1, 0, 3.

How often is nu2(n) equal to 0?

For half of all whole numbers - precisely the odd numbers, which have no factor of two at all.

What is the largest ruler value up to 16?

4, first reached at n = 16, the first multiple of 2^4.

Is nu2 the same as the 2-adic valuation?

Yes. nu2(n) is exactly the 2-adic valuation v2(n): the exponent of 2 in the factorisation of n.

Sources

Related calculators

Absolute Value Calculator
Absolute values and the distance between two numbers on the number line: |a|, |b| and |a - b|, with the sign-stripping logic spelled out.
Abundancy Index Calculator
Compute the abundancy index sigma(n)/n of a number, classify it as deficient, perfect or abundant, and test whether two numbers are friendly.
Abundant Number Calculator
Classify any whole number as abundant, perfect or deficient from its aliquot sum: the sum of its proper divisors, the abundance or deficiency against the number itself, and the s(n)/n ratio that puts perfect numbers exactly at one.