Theorem: Nat.sq_mul_squarefree_of_pos
File: Mathlib/Data/Nat/Squarefree.lean

tactic: set S := (Finset.range (n + 1)).filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2)
state_before: s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: have hSne : S.Nonempty := by
  use 1
  have h1 : 0 < n ∧ ∃ x : ℕ, 1 = x ^ 2 := ⟨hn, ⟨1, (one_pow 2).symm⟩⟩
  simp [S, h1]
state_before: s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: let s := Finset.max' S hSne
state_before: s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: have hs : s ∈ S := Finset.max'_mem S hSne
state_before: s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
hs : s ∈ S
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: simp only [S, Finset.mem_filter, Finset.mem_range] at hs
state_before: s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
hs : s ∈ S
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
hs : s < n + 1 ∧ s ∣ n ∧ ∃ x, s = x ^ 2
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: obtain ⟨-, ⟨a, hsa⟩, ⟨b, hsb⟩⟩ := hs
state_before: s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
hs : s < n + 1 ∧ s ∣ n ∧ ∃ x, s = x ^ 2
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: case intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a : ℕ
hsa : n = s * a
b : ℕ
hsb : s = b ^ 2
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: rw [hsa] at hn
state_before: case intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a : ℕ
hsa : n = s * a
b : ℕ
hsb : s = b ^ 2
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: case intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a : ℕ
hn : 0 < s * a
hsa : n = s * a
b : ℕ
hsb : s = b ^ 2
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: obtain ⟨hlts, hlta⟩ := CanonicallyOrderedCommSemiring.mul_pos.mp hn
state_before: case intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a : ℕ
hn : 0 < s * a
hsa : n = s * a
b : ℕ
hsb : s = b ^ 2
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: case intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a : ℕ
hn : 0 < s * a
hsa : n = s * a
b : ℕ
hsb : s = b ^ 2
hlts : 0 < s
hlta : 0 < a
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: rw [hsb] at hsa hn hlts
state_before: case intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a : ℕ
hn : 0 < s * a
hsa : n = s * a
b : ℕ
hsb : s = b ^ 2
hlts : 0 < s
hlta : 0 < a
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: case intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a

tactic: refine ⟨a, b, hlta, (pow_pos_iff two_ne_zero).mp hlts, hsa.symm, ?_⟩
state_before: case intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
⊢ ∃ a b, 0 < a ∧ 0 < b ∧ b ^ 2 * a = n ∧ Squarefree a
state_after: case intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
⊢ Squarefree a

tactic: rintro x ⟨y, hy⟩
state_before: case intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
⊢ Squarefree a
state_after: case intro.intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
⊢ IsUnit x

tactic: rw [Nat.isUnit_iff]
state_before: case intro.intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
⊢ IsUnit x
state_after: case intro.intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
⊢ x = 1

tactic: by_contra hx
state_before: case intro.intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
⊢ x = 1
state_after: case intro.intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ False

tactic: refine Nat.lt_le_asymm ?_ (Finset.le_max' S ((b * x) ^ 2) ?_)
state_before: case intro.intro.intro.intro.intro.intro
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ False
state_after: case intro.intro.intro.intro.intro.intro.refine_1
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ S.max' ⋯ < (b * x) ^ 2

case intro.intro.intro.intro.intro.intro.refine_2
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ (b * x) ^ 2 ∈ S

tactic: use 1
state_before: s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
⊢ S.Nonempty
state_after: case h
s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
⊢ 1 ∈ S

tactic: have h1 : 0 < n ∧ ∃ x : ℕ, 1 = x ^ 2 := ⟨hn, ⟨1, (one_pow 2).symm⟩⟩
state_before: case h
s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
⊢ 1 ∈ S
state_after: case h
s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
h1 : 0 < n ∧ ∃ x, 1 = x ^ 2
⊢ 1 ∈ S

tactic: simp [S, h1]
state_before: case h
s : Finset ℕ
m n✝ p n : ℕ
hn : 0 < n
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
h1 : 0 < n ∧ ∃ x, 1 = x ^ 2
⊢ 1 ∈ S
state_after: no goals

tactic: convert lt_mul_of_one_lt_right hlts
  (one_lt_pow two_ne_zero (one_lt_iff_ne_zero_and_ne_one.mpr ⟨fun h => by simp_all, hx⟩))
  using 1
state_before: case intro.intro.intro.intro.intro.intro.refine_1
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ S.max' ⋯ < (b * x) ^ 2
state_after: case h.e'_4
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ (b * x) ^ 2 = b ^ 2 * x ^ 2

tactic: rw [mul_pow]
state_before: case h.e'_4
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ (b * x) ^ 2 = b ^ 2 * x ^ 2
state_after: no goals

tactic: simp_all
state_before: s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
h : x = 0
⊢ False
state_after: no goals

tactic: simp_rw [S, hsa, Finset.mem_filter, Finset.mem_range]
state_before: case intro.intro.intro.intro.intro.intro.refine_2
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ (b * x) ^ 2 ∈ S
state_after: case intro.intro.intro.intro.intro.intro.refine_2
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ (b * x) ^ 2 < b ^ 2 * a + 1 ∧ (b * x) ^ 2 ∣ b ^ 2 * a ∧ ∃ x_1, (b * x) ^ 2 = x_1 ^ 2

tactic: refine ⟨Nat.lt_succ_iff.mpr (le_of_dvd hn ?_), ?_, ⟨b * x, rfl⟩⟩ <;> use y <;> rw [hy] <;> ring
state_before: case intro.intro.intro.intro.intro.intro.refine_2
s✝ : Finset ℕ
m n✝ p n : ℕ
S : Finset ℕ := filter (fun s => s ∣ n ∧ ∃ x, s = x ^ 2) (range (n + 1))
hSne : S.Nonempty
s : ℕ := S.max' hSne
a b : ℕ
hn : 0 < b ^ 2 * a
hsa : n = b ^ 2 * a
hsb : s = b ^ 2
hlts : 0 < b ^ 2
hlta : 0 < a
x y : ℕ
hy : a = x * x * y
hx : ¬x = 1
⊢ (b * x) ^ 2 < b ^ 2 * a + 1 ∧ (b * x) ^ 2 ∣ b ^ 2 * a ∧ ∃ x_1, (b * x) ^ 2 = x_1 ^ 2
state_after: no goals