Documentation

CslibFrontier.Algorithms.WordRAM.NatSqrt

A charged native integer square root #

Main definitions #

Main results #

@[reducible, inline]

Input integer, preserved.

@[reducible, inline]

Current root and output.

@[reducible, inline]

Successor of the current root.

@[reducible, inline]

Square of the successor.

@[reducible, inline]

Constant one.

@[reducible, inline]

Input plus one.

Advance the root and compute the next square.

A fixed program for the integer square root, with all square computations charged.

theorem Cslib.Algorithms.WordRAM.NatSqrt.exec {w : ℕ} (x : WordRAM.Data 6) (hfit : WordRAM.Fits w (4 * x.regs arg + 4)) :
∃ (y : WordRAM.Data 6), WordRAM.ExecWithin w program x y (5 * (x.regs arg).sqrt + 7) ∧ y.regs root = (x.regs arg).sqrt ∧ y.regs arg = x.regs arg ∧ y.mem = x.mem ∧ y.regs one = 1

Compute Nat.sqrt n in at most 5 sqrt(n) + 7 instructions, preserving input and memory. The linear bound 4n + 4 covers the final unsuccessful square as well as all counters.

theorem Cslib.Algorithms.WordRAM.NatSqrt.exec_rename {r w : ℕ} (f : Fin 6 → Fin r) (hf : Function.Injective f) (x : WordRAM.Data r) (hfit : WordRAM.Fits w (4 * x.regs (f arg) + 4)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (WordRAM.Code.rename f program) x y (5 * (x.regs (f arg)).sqrt + 7) ∧ y.regs (f root) = (x.regs (f arg)).sqrt ∧ y.regs (f arg) = x.regs (f arg) ∧ y.mem = x.mem ∧ y.regs (f one) = 1

Run successive squares on any injectively chosen registers.