-- The square root of a natural as a real, its elementary bounds, and its -- irrationality when the natural is not a square: no natural x and -- positive y have x = y √d. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.NumberTheory.SqrtNonsquare import Grace.Cumulus.NumberTheory.IntegerPrime import Grace.Cumulus.Real.Sqrt import Grace.Cumulus.Real.RootTwo -- The square root of a natural, total and nonnegative. def sqrtNat (d : Nat) : Real := realSqrt(realNat(d)) proof have nonneg := realNat.nonnegative at d unfold sqrtNat exact realSqrt.real with nonneg theorem sqrtNat.nonnegative (d : Nat) shows (0:R) ≤ sqrtNat(d) proof have nonneg := realNat.nonnegative at d unfold sqrtNat exact realSqrt.nonneg with nonneg theorem sqrtNat.square (d : Nat) shows sqrtNat(d) * sqrtNat(d) = realNat(d) proof have nonneg := realNat.nonnegative at d unfold sqrtNat exact realSqrt.square with nonneg theorem sqrtNat.of_square (k : Nat) shows sqrtNat(k * k) = realNat(k) proof have rootNonneg := sqrtNat.nonnegative at k * k have kNonneg := realNat.nonnegative at k have square := sqrtNat.square at k * k have product := realNat.mul at k, k rw product at square exact realMulValue.eq_of_square_eq with rootNonneg, kNonneg, square -- Bounds: √d is positive for d > 0, above 1 for d > 1, and at most d. theorem sqrtNat.positive (d : Nat) requires positive : 0 < d shows (0:R) < sqrtNat(d) proof have square := sqrtNat.square at d have dPositive := realNat.lt_of_lt at 0, d with positive rw realNat.zero at dPositive have rootNonzero : sqrtNat(d) ≠ 0 intro zero rw zero at square have zeroSquare := realMulValue.zero_left with realZero.real rw zeroSquare at square rw ← square at dPositive linarith exact Real.positive_of_nonnegative_of_ne_zero with * theorem sqrtNat.gt_one (d : Nat) requires above : 1 < d shows (1:R) < sqrtNat(d) proof have rootNonneg := sqrtNat.nonnegative at d have square := sqrtNat.square at d have dAbove := realNat.lt_of_lt at 1, d with above rw realNat.one at dAbove have notLe : ¬(sqrtNat(d) ≤ 1) intro le have squares := realMulValue.square_le_of_le with rootNonneg, le have unit := realMulValue.one_right with realOne.real rw [square, unit] at squares linarith exact realLess.of_not_le with * -- √d ≤ d for d ≥ 1, as d ≤ d². theorem sqrtNat.le_self (d : Nat) requires positive : 0 < d shows sqrtNat(d) ≤ realNat(d) proof have dNonneg := realNat.nonnegative at d have square := sqrtNat.square at d have atLeastOne : 1 ≤ d by arith have oneLe := realNat.le_of_natural_le at 1, d with atLeastOne rw realNat.one at oneLe have scaled := realLe.mul_left_of_nonnegative with oneLe, dNonneg have unit := realMulValue.one_right at realNat(d) rw unit at scaled have squares : sqrtNat(d) * sqrtNat(d) ≤ realNat(d) * realNat(d) rw square exact scaled exact realMulValue.le_of_square_le with * -- Irrationality: squaring x = y √d gives x² = d y² in ℕ by -- realNat.injective, so d is a square (IsSquare.of_square_multiple). theorem sqrtNat.irrational (d x y : Nat) requires nonsquare : ¬IsSquare(d) requires positive : 0 < y shows (realNat(x):R) ≠ (realNat(y):R) * sqrtNat(d) proof intro equation have square := sqrtNat.square at d have squares : realNat(x) * realNat(x) = (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d)) rw equation rfl have expand : (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d)) = (sqrtNat(d) * sqrtNat(d)) * (realNat(y) * realNat(y)) ring rw [expand, square] at squares have left : realNat(x * x) = realNat(x) * realNat(x) exact realNat.mul at x, x have right : realNat(d * (y * y)) = realNat(d) * (realNat(y) * realNat(y)) rw [realNat.mul, realNat.mul] rfl have natural : realNat(x * x) = realNat(d * (y * y)) by rwa [left, right] have equalNaturals := realNat.injective with natural have isSquare := IsSquare.of_square_multiple with positive, equalNaturals exact nonsquare with isSquare