Documentation

CslibFrontier.Foundations.Data.Nat.LogStar

Towers of twos and the iterated logarithm #

The tower Nat.twoTower k = 2 ^ 2 ^ ⋯ ^ 2 (k twos, twoTower 0 = 1) and its inverse, the iterated logarithm Nat.logStar n = log* n: the number of times ⌈log₂ ·⌉ must be applied to n to reach a value at most 1. Both are computable by structural recursion, so small values follow by decide. Mathlib already has the tower as hyperoperation 4 2 (and no iterated logarithm); Nat.twoTower_eq_hyperoperation identifies the two. The structural copy is kept because hyperoperation is defined by well-founded recursion, on which decide and kernel reduction get stuck. The characterization log* n ≤ k ↔ n ≤ twoTower k (Nat.logStar_le_iff) is the main interface; the recursion log* n = log* ⌈log₂ n⌉ + 1 for n ≥ 2 is Nat.logStar_of_two_le.

Main definitions #

Main results #

The tower of twos 2 ↑↑ k: twoTower 0 = 1 and twoTower (k + 1) = 2 ^ twoTower k.

This equals Mathlib's hyperoperation 4 2 (Nat.twoTower_eq_hyperoperation). It is redefined by structural recursion so that decide and kernel reduction can evaluate it; hyperoperation uses well-founded recursion and does not reduce.

def Nat.logStarAux :
ℕ → ℕ → ℕ

Iterated ceiling logarithm with a fuel argument; logStar supplies enough fuel.

def Nat.logStar (n : ℕ) :

The iterated logarithm log* n: the number of times ⌈log₂ ·⌉ must be applied to n to reach a value at most 1. Equivalently (Nat.logStar_le_iff), the least k with n ≤ 2 ↑↑ k.

@[simp]
theorem Nat.twoTower_succ (k : ℕ) :
(k + 1).twoTower = 2 ^ k.twoTower

The tower is Mathlib's fourth hyperoperation with base 2.

theorem Nat.lt_twoTower (k : ℕ) :
theorem Nat.logStar_le_iff {n k : ℕ} :

log* n ≤ k exactly when n ≤ 2 ↑↑ k.

theorem Nat.twoTower_logStar_pred_lt {n : ℕ} (hn : 1 < n) :

log* n is the least k with n ≤ 2 ↑↑ k: the tower one level lower is below n.

@[simp]

log* steps up just after each tower: log* (2 ↑↑ k + 1) = k + 1.

theorem Nat.logStar_of_le_one {n : ℕ} (hn : n ≤ 1) :
theorem Nat.logStar_of_two_le {n : ℕ} (hn : 2 ≤ n) :
n.logStar = (clog 2 n).logStar + 1

The recursion log* n = log* ⌈log₂ n⌉ + 1 for n ≥ 2.

theorem Nat.iterate_two_pow_le_twoTower (k c : ℕ) :
(fun (x : ℕ) => 2 ^ x)^[k] c ≤ (k + c.logStar).twoTower

Iterating c ↦ 2 ^ c k times from c stays below the tower of height k + log* c.

Bound log* x in terms of three iterations of ⌈log₂ ·⌉.