Grace/Cumulus/NumberTheory/SqrtNonsquare.g17
Imports
Grace.Cumulus.NumberTheory.IntegerPrime · Grace.Cumulus.Real.Sqrt · Grace.Cumulus.Real.RootTwo
Declarations
- sqrtNat def
- sqrtNat.nonnegative theorem derivation
- sqrtNat.square theorem derivation
- sqrtNat.of_square theorem derivation
- sqrtNat.positive theorem derivation
- sqrtNat.gt_one theorem derivation
- sqrtNat.le_self theorem derivation
- sqrtNat.irrational theorem derivation
Source
1-- The square root of a natural as a real, its elementary bounds, and its2-- irrationality when the natural is not a square: no natural x and3-- positive y have x = y √d.4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.NumberTheory.SqrtNonsquare10import Grace.Cumulus.NumberTheory.IntegerPrime11import Grace.Cumulus.Real.Sqrt12import Grace.Cumulus.Real.RootTwo1314-- The square root of a natural, total and nonnegative.1516def sqrtNat (d : Nat) : Real := realSqrt(realNat(d))17proof18 have nonneg := realNat.nonnegative at d19 unfold sqrtNat20 exact realSqrt.real with nonneg2122theorem sqrtNat.nonnegative (d : Nat)23 shows (0:R) ≤ sqrtNat(d)24proof25 have nonneg := realNat.nonnegative at d26 unfold sqrtNat27 exact realSqrt.nonneg with nonneg2829theorem sqrtNat.square (d : Nat)30 shows sqrtNat(d) * sqrtNat(d) = realNat(d)31proof32 have nonneg := realNat.nonnegative at d33 unfold sqrtNat34 exact realSqrt.square with nonneg3536theorem sqrtNat.of_square (k : Nat)37 shows sqrtNat(k * k) = realNat(k)38proof39 have rootNonneg := sqrtNat.nonnegative at k * k40 have kNonneg := realNat.nonnegative at k41 have square := sqrtNat.square at k * k42 have product := realNat.mul at k, k43 rw product at square44 exact realMulValue.eq_of_square_eq with rootNonneg, kNonneg, square4546-- Bounds: √d is positive for d > 0, above 1 for d > 1, and at most d.4748theorem sqrtNat.positive (d : Nat)49 requires positive : 0 < d50 shows (0:R) < sqrtNat(d)51proof52 have square := sqrtNat.square at d53 have dPositive := realNat.lt_of_lt at 0, d with positive54 rw realNat.zero at dPositive55 have rootNonzero : sqrtNat(d) ≠ 056 intro zero57 rw zero at square58 have zeroSquare := realMulValue.zero_left with realZero.real59 rw zeroSquare at square60 rw ← square at dPositive61 linarith62 exact Real.positive_of_nonnegative_of_ne_zero with *6364theorem sqrtNat.gt_one (d : Nat)65 requires above : 1 < d66 shows (1:R) < sqrtNat(d)67proof68 have rootNonneg := sqrtNat.nonnegative at d69 have square := sqrtNat.square at d70 have dAbove := realNat.lt_of_lt at 1, d with above71 rw realNat.one at dAbove72 have notLe : ¬(sqrtNat(d) ≤ 1)73 intro le74 have squares := realMulValue.square_le_of_le with rootNonneg, le75 have unit := realMulValue.one_right with realOne.real76 rw [square, unit] at squares77 linarith78 exact realLess.of_not_le with *7980-- √d ≤ d for d ≥ 1, as d ≤ d².81theorem sqrtNat.le_self (d : Nat)82 requires positive : 0 < d83 shows sqrtNat(d) ≤ realNat(d)84proof85 have dNonneg := realNat.nonnegative at d86 have square := sqrtNat.square at d87 have atLeastOne : 1 ≤ d by arith88 have oneLe := realNat.le_of_natural_le at 1, d with atLeastOne89 rw realNat.one at oneLe90 have scaled := realLe.mul_left_of_nonnegative with oneLe, dNonneg91 have unit := realMulValue.one_right at realNat(d)92 rw unit at scaled93 have squares : sqrtNat(d) * sqrtNat(d) ≤ realNat(d) * realNat(d)94 rw square95 exact scaled96 exact realMulValue.le_of_square_le with *9798-- Irrationality: squaring x = y √d gives x² = d y² in ℕ by99-- realNat.injective, so d is a square (IsSquare.of_square_multiple).100101theorem sqrtNat.irrational (d x y : Nat)102 requires nonsquare : ¬IsSquare(d)103 requires positive : 0 < y104 shows (realNat(x):R) ≠ (realNat(y):R) * sqrtNat(d)105proof106 intro equation107 have square := sqrtNat.square at d108 have squares : realNat(x) * realNat(x) =109 (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d))110 rw equation111 rfl112 have expand : (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d)) =113 (sqrtNat(d) * sqrtNat(d)) * (realNat(y) * realNat(y))114 ring115 rw [expand, square] at squares116 have left : realNat(x * x) = realNat(x) * realNat(x)117 exact realNat.mul at x, x118 have right : realNat(d * (y * y)) = realNat(d) * (realNat(y) * realNat(y))119 rw [realNat.mul, realNat.mul]120 rfl121 have natural : realNat(x * x) = realNat(d * (y * y)) by rwa [left, right]122 have equalNaturals := realNat.injective with natural123 have isSquare := IsSquare.of_square_multiple with positive, equalNaturals124 exact nonsquare with isSquare125