The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Nat.NthRoot.Defs

2 declarations. Mathlib docs · Source

Definition of Nat.nthRoot

In this file we define Nat.nthRoot n a to be the floor of the nth root of a. The function is defined in terms of natural numbers with no dependencies outside of prelude.

def Nat.nthRoot E1 #

ℕ → ℕ → ℕ

Nat.nthRoot n a = ⌊(a : ℝ) ^ (1 / n : ℝ)⌋₊ defined in terms of natural numbers.

We use Newton's method to find a root of $x^n = a$, so it converges superexponentially fast.

def Nat.nthRoot.go E1 #

ℕ → ℕ → ℕ → ℕ → ℕ

Auxiliary definition for Nat.nthRoot.

Given natural numbers n, a, fuel, guess such that ⌊(a : ℝ) ^ (1 / (n + 2) : ℝ)⌋₊ ≤ guess ≤ fuel, returns ⌊(a : ℝ) ^ (1 / (n + 2) : ℝ)⌋₊.

The auxiliary number guess is the current approximation in Newton's method, tracked in the arguments so that the definition uses a tail recursion which is unfolded into a loop by the compiler.

The auxiliary number fuel is an upper estimate on the number of steps in Newton's method. Any number fuel ≥ guess is guaranteed to work, but smaller numbers may work as well. If fuel is too small, then Nat.nthRoot.go returns the result of the fuelth step of Newton's method.