/-- Suppose that $f$ is holomorphic in an open set $\Omega$. Prove that if $\text{Re}(f)$ is constant, then $f$ is constant.-/

theorem exercise_1_13a {f : ℂ → ℂ} (Ω : Set ℂ) (a b : Ω) (h : IsOpen Ω)
  (hf : DifferentiableOn ℂ f Ω) (hc : ∃ (c : ℝ), ∀ z ∈ Ω, (f z).re = c) :
  f a = f b :=

f : ℂ → ℂ
Ω : Set ℂ
a b : ↑Ω
h : IsOpen Ω
hf : DifferentiableOn ℂ f Ω
hc : ∃ c, ∀ z ∈ Ω, (f z).re = c
⊢ f ↑a = f ↑b

import Mathlib

open Complex Filter Function Metric Finset
open scoped BigOperators Topology

open scoped BigOperators Topology


/-- Suppose that $f$ is holomorphic in an open set $\Omega$. Prove that if $\text{Im}(f)$ is constant, then $f$ is constant.-/

theorem exercise_1_13b {f : ℂ → ℂ} (Ω : Set ℂ) (a b : Ω) (h : IsOpen Ω)
  (hf : DifferentiableOn ℂ f Ω) (hc : ∃ (c : ℝ), ∀ z ∈ Ω, (f z).im = c) :
  f a = f b :=

f : ℂ → ℂ
Ω : Set ℂ
a b : ↑Ω
h : IsOpen Ω
hf : DifferentiableOn ℂ f Ω
hc : ∃ c, ∀ z ∈ Ω, (f z).im = c
⊢ f ↑a = f ↑b

import Mathlib

open Complex Filter Function Metric Finset
open scoped BigOperators Topology


/-- Suppose that $f$ is holomorphic in an open set $\Omega$. Prove that if $|f|$ is constant, then $f$ is constant.-/

theorem exercise_1_13c {f : ℂ → ℂ} (Ω : Set ℂ) (a b : Ω) (h : IsOpen Ω)
  (hf : DifferentiableOn ℂ f Ω) (hc : ∃ (c : ℝ), ∀ z ∈ Ω, abs (f z) = c) :
  f a = f b :=

f : ℂ → ℂ
Ω : Set ℂ
a b : ↑Ω
h : IsOpen Ω
hf : DifferentiableOn ℂ f Ω
hc : ∃ c, ∀ z ∈ Ω, Complex.abs (f z) = c
⊢ f ↑a = f ↑b


/-- Prove that the power series $\sum nz^n$ does not converge on any point of the unit circle.-/

theorem exercise_1_19a (z : ℂ) (hz : abs z = 1) (s : ℕ → ℂ)
    (h : s = (λ n => ∑ i in (range n), i * z ^ i)) :
    ¬ ∃ y, Tendsto s atTop (𝓝 y) :=

z : ℂ
hz : Complex.abs z = 1
s : ℕ → ℂ
h : s = fun n => ∑ i ∈ range n, ↑i * z ^ i
⊢ ¬∃ y, Tendsto s atTop (𝓝 y)


/-- Prove that the power series $\sum zn/n^2$ converges at every point of the unit circle.-/

theorem exercise_1_19b (z : ℂ) (hz : abs z = 1) (s : ℕ → ℂ)
    (h : s = (λ n => ∑ i in (range n), i * z / i ^ 2)) :
    ∃ y, Tendsto s atTop (𝓝 y) :=

z : ℂ
hz : Complex.abs z = 1
s : ℕ → ℂ
h : s = fun n => ∑ i ∈ range n, ↑i * z / ↑i ^ 2
⊢ ∃ y, Tendsto s atTop (𝓝 y)


/-- Prove that the power series $\sum zn/n$ converges at every point of the unit circle except $z = 1$.-/

theorem exercise_1_19c (z : ℂ) (hz : abs z = 1) (hz2 : z ≠ 1) (s : ℕ → ℂ)
    (h : s = (λ n => ∑ i in (range n), i * z / i)) :
    ∃ z, Tendsto s atTop (𝓝 z) :=

z : ℂ
hz : Complex.abs z = 1
hz2 : z ≠ 1
s : ℕ → ℂ
h : s = fun n => ∑ i ∈ range n, ↑i * z / ↑i
⊢ ∃ z, Tendsto s atTop (𝓝 z)


/-- Suppose $f$ is continuous in a region $\Omega$. Prove that any two primitives of $f$ (if they exist) differ by a constant.-/

theorem exercise_1_26
  (f F₁ F₂ : ℂ → ℂ) (Ω : Set ℂ) (h1 : IsOpen Ω) (h2 : IsConnected Ω)
  (hF₁ : DifferentiableOn ℂ F₁ Ω) (hF₂ : DifferentiableOn ℂ F₂ Ω)
  (hdF₁ : ∀ x ∈ Ω, deriv F₁ x = f x) (hdF₂ : ∀ x ∈ Ω, deriv F₂ x = f x)
  : ∃ c : ℂ, ∀ x, F₁ x = F₂ x + c :=

f F₁ F₂ : ℂ → ℂ
Ω : Set ℂ
h1 : IsOpen Ω
h2 : IsConnected Ω
hF₁ : DifferentiableOn ℂ F₁ Ω
hF₂ : DifferentiableOn ℂ F₂ Ω
hdF₁ : ∀ x ∈ Ω, deriv F₁ x = f x
hdF₂ : ∀ x ∈ Ω, deriv F₂ x = f x
⊢ ∃ c, ∀ (x : ℂ), F₁ x = F₂ x + c


/-- Show that $\int_{0}^{\infty} \frac{\sin x}{x} d x=\frac{\pi}{2}$.-/

theorem exercise_2_2 :
  Tendsto (λ y => ∫ x in (0 : ℝ)..y, Real.sin x / x) atTop (𝓝 (Real.pi / 2)) :=

⊢ Tendsto (fun y => ∫ (x : ℝ) in 0 ..y, x.sin / x) atTop (𝓝 (Real.pi / 2))


/-- Let $\Omega$ be a bounded open subset of $\mathbb{C}$, and $\varphi: \Omega \rightarrow \Omega$ a holomorphic function. Prove that if there exists a point $z_{0} \in \Omega$ such that $\varphi\left(z_{0}\right)=z_{0} \quad \text { and } \quad \varphi^{\prime}\left(z_{0}\right)=1$ then $\varphi$ is linear.-/

theorem exercise_2_9
  {f : ℂ → ℂ} (Ω : Set ℂ) (b : Bornology.IsBounded Ω) (h : IsOpen Ω)
  (hf : DifferentiableOn ℂ f Ω) (z : Ω) (hz : f z = z) (h'z : deriv f z = 1) :
  ∃ (f_lin : ℂ →L[ℂ] ℂ), ∀ x ∈ Ω, f x = f_lin x :=

f : ℂ → ℂ
Ω : Set ℂ
b : Bornology.IsBounded Ω
h : IsOpen Ω
hf : DifferentiableOn ℂ f Ω
z : ↑Ω
hz : f ↑z = ↑z
h'z : deriv f ↑z = 1
⊢ ∃ f_lin, ∀ x ∈ Ω, f x = f_lin x


/-- Suppose $f$ is an analytic function defined everywhere in $\mathbb{C}$ and such that for each $z_0 \in \mathbb{C}$ at least one coefficient in the expansion $f(z) = \sum_{n=0}^\infty c_n(z - z_0)^n$ is equal to 0. Prove that $f$ is a polynomial.-/

theorem exercise_2_13 {f : ℂ → ℂ}
    (hf : ∀ z₀ : ℂ, ∃ (s : Set ℂ) (c : ℕ → ℂ), IsOpen s ∧ z₀ ∈ s ∧
      ∀ z ∈ s, Tendsto (λ n => ∑ i in range n, (c i) * (z - z₀)^i) atTop (𝓝 (f z₀))
      ∧ ∃ i, c i = 0) :
    ∃ (c : ℕ → ℂ) (n : ℕ), f = λ z => ∑ i in range n, (c i) * z ^ n :=

f : ℂ → ℂ
hf :
  ∀ (z₀ : ℂ),
    ∃ s c,
      IsOpen s ∧ z₀ ∈ s ∧ ∀ z ∈ s, Tendsto (fun n => ∑ i ∈ range n, c i * (z - z₀) ^ i) atTop (𝓝 (f z₀)) ∧ ∃ i, c i = 0
⊢ ∃ c n, f = fun z => ∑ i ∈ range n, c i * z ^ n


/-- Show that $ \int_{-\infty}^{\infty} \frac{\cos x}{x^2 + a^2} dx = \pi \frac{e^{-a}}{a}$ for $a > 0$.-/

theorem exercise_3_3 (a : ℝ) (ha : 0 < a) :
    Tendsto (λ y => ∫ x in -y..y, Real.cos x / (x ^ 2 + a ^ 2))
    atTop (𝓝 (Real.pi * (Real.exp (-a) / a))) :=

a : ℝ
ha : 0 < a
⊢ Tendsto (fun y => ∫ (x : ℝ) in -y..y, x.cos / (x ^ 2 + a ^ 2)) atTop (𝓝 (Real.pi * ((-a).exp / a)))


/-- Show that $ \int_{-\infty}^{\infty} \frac{x \sin x}{x^2 + a^2} dx = \pi e^{-a}$ for $a > 0$.-/

theorem exercise_3_4 (a : ℝ) (ha : 0 < a) :
    Tendsto (λ y => ∫ x in -y..y, x * Real.sin x / (x ^ 2 + a ^ 2))
    atTop (𝓝 (Real.pi * (Real.exp (-a)))) :=

a : ℝ
ha : 0 < a
⊢ Tendsto (fun y => ∫ (x : ℝ) in -y..y, x * x.sin / (x ^ 2 + a ^ 2)) atTop (𝓝 (Real.pi * (-a).exp))


/-- Show that $\int_0^1 \log(\sin \pi x) dx = - \log 2$.-/

theorem exercise_3_9 : ∫ x in (0 : ℝ)..(1 : ℝ),
    Real.log (Real.sin (Real.pi * x)) = - Real.log 2 :=

⊢ ∫ (x : ℝ) in 0 ..1, (Real.pi * x).sin.log = -Real.log 2


/-- Prove that all entire functions that are also injective take the form $f(z) = az + b$, $a, b \in \mathbb{C}$ and $a \neq 0$.-/

theorem exercise_3_14 {f : ℂ → ℂ} (hf : Differentiable ℂ f)
    (hf_inj : Function.Injective f) :
    ∃ (a b : ℂ), f = (λ z => a * z + b) ∧ a ≠ 0 :=

f : ℂ → ℂ
hf : Differentiable ℂ f
hf_inj : Injective f
⊢ ∃ a b, (f = fun z => a * z + b) ∧ a ≠ 0


/-- Show that there is no holomorphic function $f$ in the unit disc $D$ that extends continuously to $\partial D$ such that $f(z) = 1/z$ for $z \in \partial D$.-/

theorem exercise_3_22 (D : Set ℂ) (hD : D = ball 0 1) (f : ℂ → ℂ)
    (hf : DifferentiableOn ℂ f D) (hfc : ContinuousOn f (closure D)) :
    ¬ ∀ z ∈ (sphere (0 : ℂ) 1), f z = 1 / z :=

D : Set ℂ
hD : D = ball 0 1
f : ℂ → ℂ
hf : DifferentiableOn ℂ f D
hfc : ContinuousOn f (closure D)
⊢ ¬∀ z ∈ sphere 0 1, f z = 1 / z


/-- Prove that if $f$ is holomorphic in the unit disc, bounded and not identically zero, and $z_{1}, z_{2}, \ldots, z_{n}, \ldots$ are its zeros $\left(\left|z_{k}\right|<1\right)$, then $\sum_{n}\left(1-\left|z_{n}\right|\right)<\infty$.-/

theorem exercise_5_1 (f : ℂ → ℂ) (hf : DifferentiableOn ℂ f (ball 0 1))
  (hb : Bornology.IsBounded (Set.range f)) (h0 : f ≠ 0) (zeros : ℕ → ℂ) (hz : ∀ n, f (zeros n) = 0)
  (hzz : Set.range zeros = {z | f z = 0 ∧ z ∈ (ball (0 : ℂ) 1)}) :
  ∃ (z : ℂ), Tendsto (λ n => (∑ i in range n, (1 - zeros i))) atTop (𝓝 z) :=

f : ℂ → ℂ
hf : DifferentiableOn ℂ f (ball 0 1)
hb : Bornology.IsBounded (Set.range f)
h0 : f ≠ 0
zeros : ℕ → ℂ
hz : ∀ (n : ℕ), f (zeros n) = 0
hzz : Set.range zeros = {z | f z = 0 ∧ z ∈ ball 0 1}
⊢ ∃ z, Tendsto (fun n => ∑ i ∈ range n, (1 - zeros i)) atTop (𝓝 z)


/-- If $r$ is rational $(r \neq 0)$ and $x$ is irrational, prove that $r+x$ is irrational.-/

theorem exercise_1_1a
  (x : ℝ) (y : ℚ) :
  ( Irrational x ) -> Irrational ( x + y ) :=

x : ℝ
y : ℚ
⊢ Irrational x → Irrational (x + ↑y)

import Mathlib

open Topology Filter Real Complex TopologicalSpace Finset
open scoped BigOperators


/-- If $r$ is rational $(r \neq 0)$ and $x$ is irrational, prove that $rx$ is irrational.-/

theorem exercise_1_1b
(x : ℝ)
(y : ℚ)
(h : y ≠ 0)
: ( Irrational x ) -> Irrational ( x * y ) :=

x : ℝ
y : ℚ
h : y ≠ 0
⊢ Irrational x → Irrational (x * ↑y)


/-- Prove that there is no rational number whose square is $12$.-/

theorem exercise_1_2 : ¬ ∃ (x : ℚ), ( x ^ 2 = 12 ) :=


/-- Let $E$ be a nonempty subset of an ordered set; suppose $\alpha$ is a lower bound of $E$ and $\beta$ is an upper bound of $E$. Prove that $\alpha \leq \beta$.-/

theorem exercise_1_4
(α : Type*) [PartialOrder α]
(s : Set α)
(x y : α)
(h₀ : Set.Nonempty s)
(h₁ : x ∈ lowerBounds s)
(h₂ : y ∈ upperBounds s)
: x ≤ y :=

α : Type u_1
inst✝ : PartialOrder α
s : Set α
x y : α
h₀ : s.Nonempty
h₁ : x ∈ lowerBounds s
h₂ : y ∈ upperBounds s
⊢ x ≤ y


/-- Let $A$ be a nonempty set of real numbers which is bounded below. Let $-A$ be the set of all numbers $-x$, where $x \in A$. Prove that $\inf A=-\sup (-A)$.-/

theorem exercise_1_5 (A minus_A : Set ℝ) (hA : A.Nonempty)
  (hA_bdd_below : BddBelow A) (hminus_A : minus_A = {x | -x ∈ A}) :
  Inf A = Sup minus_A :=

A minus_A : Set ℝ
hA : A.Nonempty
hA_bdd_below : BddBelow A
hminus_A : minus_A = {x | -x ∈ A}
⊢ Inf ↑A = Sup ↑minus_A


/-- Prove that no order can be defined in the complex field that turns it into an ordered field.-/

theorem exercise_1_8 : ¬ ∃ (r : ℂ → ℂ → Prop), IsLinearOrder ℂ r :=


/-- If $z$ is a complex number, prove that there exists an $r\geq 0$ and a complex number $w$ with $| w | = 1$ such that $z = rw$.-/

theorem exercise_1_11a (z : ℂ) :
  ∃ (r : ℝ) (w : ℂ), abs w = 1 ∧ z = r * w :=

z : ℂ
⊢ ∃ r w, Complex.abs w = 1 ∧ z = ↑r * w


/-- If $z_1, \ldots, z_n$ are complex, prove that $|z_1 + z_2 + \ldots + z_n| \leq |z_1| + |z_2| + \cdots + |z_n|$.-/

theorem exercise_1_12 (n : ℕ) (f : ℕ → ℂ) :
  abs (∑ i in range n, f i) ≤ ∑ i in range n, abs (f i) :=

n : ℕ
f : ℕ → ℂ
⊢ Complex.abs (∑ i ∈ range n, f i) ≤ ∑ i ∈ range n, Complex.abs (f i)


/-- If $x, y$ are complex, prove that $||x|-|y|| \leq |x-y|$.-/

theorem exercise_1_13 (x y : ℂ) :
  |(abs x) - (abs y)| ≤ abs (x - y) :=

x y : ℂ
⊢ |Complex.abs x - Complex.abs y| ≤ Complex.abs (x - y)


/-- If $z$ is a complex number such that $|z|=1$, that is, such that $z \bar{z}=1$, compute $|1+z|^{2}+|1-z|^{2}$.-/

z : ℂ
h : Complex.abs z = 1
⊢ Complex.abs (1 + z) ^ 2 + Complex.abs (1 - z) ^ 2 = 4


/-- Suppose $k \geq 3, x, y \in \mathbb{R}^k, |x - y| = d > 0$, and $r > 0$. Prove that if $2r > d$, there are infinitely many $z \in \mathbb{R}^k$ such that $|z-x|=|z-y|=r$.-/

theorem exercise_1_16a
  (n : ℕ)
  (d r : ℝ)
  (x y z : EuclideanSpace ℝ (Fin n)) -- R^n
  (h₁ : n ≥ 3)
  (h₂ : ‖x - y‖ = d)
  (h₃ : d > 0)
  (h₄ : r > 0)
  (h₅ : 2 * r > d)
  : Set.Infinite {z : EuclideanSpace ℝ (Fin n) | ‖z - x‖ = r ∧ ‖z - y‖ = r} :=

n : ℕ
d r : ℝ
x y z : EuclideanSpace ℝ (Fin n)
h₁ : n ≥ 3
h₂ : ‖x - y‖ = d
h₃ : d > 0
h₄ : r > 0
h₅ : 2 * r > d
⊢ {z | ‖z - x‖ = r ∧ ‖z - y‖ = r}.Infinite


/-- Prove that $|\mathbf{x}+\mathbf{y}|^{2}+|\mathbf{x}-\mathbf{y}|^{2}=2|\mathbf{x}|^{2}+2|\mathbf{y}|^{2}$ if $\mathbf{x} \in R^{k}$ and $\mathbf{y} \in R^{k}$.-/

theorem exercise_1_17
  (n : ℕ)
  (x y : EuclideanSpace ℝ (Fin n)) -- R^n
  : ‖x + y‖^2 + ‖x - y‖^2 = 2*‖x‖^2 + 2*‖y‖^2 :=

n : ℕ
x y : EuclideanSpace ℝ (Fin n)
⊢ ‖x + y‖ ^ 2 + ‖x - y‖ ^ 2 = 2 * ‖x‖ ^ 2 + 2 * ‖y‖ ^ 2


/-- If $k \geq 2$ and $\mathbf{x} \in R^{k}$, prove that there exists $\mathbf{y} \in R^{k}$ such that $\mathbf{y} \neq 0$ but $\mathbf{x} \cdot \mathbf{y}=0$-/

theorem exercise_1_18a
  (n : ℕ)
  (h : n > 1)
  (x : EuclideanSpace ℝ (Fin n)) -- R^n
  : ∃ (y : EuclideanSpace ℝ (Fin n)), y ≠ 0 ∧ (inner x y) = (0 : ℝ) :=

n : ℕ
h : n > 1
x : EuclideanSpace ℝ (Fin n)
⊢ ∃ y, y ≠ 0 ∧ ⟪x, y⟫_ℝ = 0


/-- If $k = 1$ and $\mathbf{x} \in R^{k}$, prove that there does not exist $\mathbf{y} \in R^{k}$ such that $\mathbf{y} \neq 0$ but $\mathbf{x} \cdot \mathbf{y}=0$-/

theorem exercise_1_18b
  : ¬ ∀ (x : ℝ), ∃ (y : ℝ), y ≠ 0 ∧ x * y = 0 :=


/-- Suppose $a, b \in R^k$. Find $c \in R^k$ and $r > 0$ such that $|x-a|=2|x-b|$ if and only if $| x - c | = r$. Prove that $3c = 4b - a$ and $3r = 2 |b - a|$.-/

theorem exercise_1_19
  (n : ℕ)
  (a b c x : EuclideanSpace ℝ (Fin n))
  (r : ℝ)
  (h₁ : r > 0)
  (h₂ : 3 • c = 4 • b - a)
  (h₃ : 3 * r = 2 * ‖x - b‖)
  : ‖x - a‖ = 2 * ‖x - b‖ ↔ ‖x - c‖ = r :=

n : ℕ
a b c x : EuclideanSpace ℝ (Fin n)
r : ℝ
h₁ : r > 0
h₂ : 3 • c = 4 • b - a
h₃ : 3 * r = 2 * ‖x - b‖
⊢ ‖x - a‖ = 2 * ‖x - b‖ ↔ ‖x - c‖ = r


/-- If $A$ and $B$ are disjoint closed sets in some metric space $X$, prove that they are separated.-/

theorem exercise_2_19a {X : Type*} [MetricSpace X]
  (A B : Set X) (hA : IsClosed A) (hB : IsClosed B) (hAB : Disjoint A B) :
  SeparatedNhds A B :=

X : Type u_1
inst✝ : MetricSpace X
A B : Set X
hA : IsClosed A
hB : IsClosed B
hAB : Disjoint A B
⊢ SeparatedNhds A B


/-- Let $X$ be a metric space in which every infinite subset has a limit point. Prove that $X$ is separable.-/

theorem exercise_2_24 {X : Type*} [MetricSpace X]
  (hX : ∀ (A : Set X), Infinite A → ∃ (x : X), x ∈ closure A) :
  SeparableSpace X :=

X : Type u_1
inst✝ : MetricSpace X
hX : ∀ (A : Set X), Infinite ↑A → ∃ x, x ∈ closure A
⊢ SeparableSpace X


/-- Prove that every compact metric space $K$ has a countable base.-/

theorem exercise_2_25 {K : Type*} [MetricSpace K] [CompactSpace K] :
  ∃ (B : Set (Set K)), Set.Countable B ∧ IsTopologicalBasis B :=

K : Type u_1
inst✝¹ : MetricSpace K
inst✝ : CompactSpace K
⊢ ∃ B, B.Countable ∧ IsTopologicalBasis B


/-- Suppose $E\subset\mathbb{R}^k$ is uncountable, and let $P$ be the set of condensation points of $E$. Prove that $P$ is perfect.-/

theorem exercise_2_27a (k : ℕ) (E P : Set (EuclideanSpace ℝ (Fin k)))
  (hE : E.Nonempty ∧ ¬ Set.Countable E)
  (hP : P = {x | ∀ U ∈ 𝓝 x, ¬ Set.Countable (P ∩ E)}) :
  IsClosed P ∧ P = {x | ClusterPt x (𝓟 P)} :=

k : ℕ
E P : Set (EuclideanSpace ℝ (Fin k))
hE : E.Nonempty ∧ ¬E.Countable
hP : P = {x | ∀ U ∈ 𝓝 x, ¬(P ∩ E).Countable}
⊢ IsClosed P ∧ P = {x | ClusterPt x (𝓟 P)}


/-- Suppose $E\subset\mathbb{R}^k$ is uncountable, and let $P$ be the set of condensation points of $E$. Prove that at most countably many points of $E$ are not in $P$.-/

theorem exercise_2_27b (k : ℕ) (E P : Set (EuclideanSpace ℝ (Fin k)))
  (hE : E.Nonempty ∧ ¬ Set.Countable E)
  (hP : P = {x | ∀ U ∈ 𝓝 x, (P ∩ E).Nonempty ∧ ¬ Set.Countable (P ∩ E)}) :
  Set.Countable (E \ P) :=

k : ℕ
E P : Set (EuclideanSpace ℝ (Fin k))
hE : E.Nonempty ∧ ¬E.Countable
hP : P = {x | ∀ U ∈ 𝓝 x, (P ∩ E).Nonempty ∧ ¬(P ∩ E).Countable}
⊢ (E \ P).Countable


/-- Prove that every closed set in a separable metric space is the union of a (possibly empty) perfect set and a set which is at most countable.-/

theorem exercise_2_28 (X : Type*) [MetricSpace X] [SeparableSpace X]
  (A : Set X) (hA : IsClosed A) :
  ∃ P₁ P₂ : Set X, A = P₁ ∪ P₂ ∧
  IsClosed P₁ ∧ P₁ = {x | ClusterPt x (𝓟 P₁)} ∧
  Set.Countable P₂ :=

X : Type u_1
inst✝¹ : MetricSpace X
inst✝ : SeparableSpace X
A : Set X
hA : IsClosed A
⊢ ∃ P₁ P₂, A = P₁ ∪ P₂ ∧ IsClosed P₁ ∧ P₁ = {x | ClusterPt x (𝓟 P₁)} ∧ P₂.Countable


/-- Prove that every open set in $\mathbb{R}$ is the union of an at most countable collection of disjoint segments.-/

theorem exercise_2_29 (U : Set ℝ) (hU : IsOpen U) :
  ∃ (f : ℕ → Set ℝ), (∀ n, ∃ a b : ℝ, f n = {x | a < x ∧ x < b}) ∧ (∀ n, f n ⊆ U) ∧
  (∀ n m, n ≠ m → f n ∩ f m = ∅) ∧
  U = ⋃ n, f n :=

U : Set ℝ
hU : IsOpen U
⊢ ∃ f,
    (∀ (n : ℕ), ∃ a b, f n = {x | a < x ∧ x < b}) ∧
      (∀ (n : ℕ), f n ⊆ U) ∧ (∀ (n m : ℕ), n ≠ m → f n ∩ f m = ∅) ∧ U = ⋃ n, f n


/-- Prove that convergence of $\left\{s_{n}\right\}$ implies convergence of $\left\{\left|s_{n}\right|\right\}$.-/

theorem exercise_3_1a
  (f : ℕ → ℝ)
  (h : ∃ (a : ℝ), Tendsto (λ (n : ℕ) => f n) atTop (𝓝 a))
  : ∃ (a : ℝ), Tendsto (λ (n : ℕ) => |f n|) atTop (𝓝 a) :=

f : ℕ → ℝ
h : ∃ a, Tendsto (fun n => f n) atTop (𝓝 a)
⊢ ∃ a, Tendsto (fun n => |f n|) atTop (𝓝 a)


/-- Prove that $\lim_{n \rightarrow \infty}\sqrt{n^2 + n} -n = 1/2$.-/

theorem exercise_3_2a
  : Tendsto (λ (n : ℝ) => (sqrt (n^2 + n) - n)) atTop (𝓝 (1/2)) :=

⊢ Tendsto (fun n => √(n ^ 2 + n) - n) atTop (𝓝 (1 / 2))


/-- If $s_{1}=\sqrt{2}$, and $s_{n+1}=\sqrt{2+\sqrt{s_{n}}} \quad(n=1,2,3, \ldots),$ prove that $\left\{s_{n}\right\}$ converges, and that $s_{n}<2$ for $n=1,2,3, \ldots$.-/

theorem exercise_3_3
  : ∃ (x : ℝ), Tendsto f atTop (𝓝 x) ∧ ∀ n, f n < 2 :=

⊢ ∃ x, Tendsto f atTop (𝓝 x) ∧ ∀ (n : ℕ), f n < 2

import Mathlib

open Topology Filter Real Complex TopologicalSpace Finset
open scoped BigOperators

noncomputable def f : ℕ → ℝ
| 0 => sqrt 2
| (n + 1) => sqrt (2 + sqrt (f n))


/-- For any two real sequences $\left\{a_{n}\right\},\left\{b_{n}\right\}$, prove that $\limsup _{n \rightarrow \infty}\left(a_{n}+b_{n}\right) \leq \limsup _{n \rightarrow \infty} a_{n}+\limsup _{n \rightarrow \infty} b_{n},$ provided the sum on the right is not of the form $\infty-\infty$.-/

theorem exercise_3_5
  (a b : ℕ → ℝ)
  (h : limsup a + limsup b ≠ 0) :
  limsup (λ n => a n + b n) ≤ limsup a + limsup b :=

a b : ℕ → ℝ
h : limsup a + limsup b ≠ 0
⊢ (limsup fun n => a n + b n) ≤ limsup a + limsup b


/-- Prove that $\lim_{n \rightarrow \infty} \sum_{i<n} a_i = \infty$, where $a_i = \sqrt{i + 1} -\sqrt{i}$.-/

theorem exercise_3_6a
: Tendsto (λ (n : ℕ) => (∑ i in range n, g i)) atTop atTop :=

⊢ Tendsto (fun n => ∑ i ∈ range n, g i) atTop atTop

import Mathlib

open Topology Filter Real Complex TopologicalSpace Finset
open scoped BigOperators

noncomputable section

def g (n : ℕ) : ℝ := sqrt (n + 1) - sqrt n


/-- Prove that the convergence of $\Sigma a_{n}$ implies the convergence of $\sum \frac{\sqrt{a_{n}}}{n}$ if $a_n\geq 0$.-/

theorem exercise_3_7
  (a : ℕ → ℝ)
  (h : ∃ y, (Tendsto (λ n => (∑ i in (range n), a i)) atTop (𝓝 y))) :
  ∃ y, Tendsto (λ n => (∑ i in (range n), sqrt (a i) / n)) atTop (𝓝 y) :=

a : ℕ → ℝ
h : ∃ y, Tendsto (fun n => ∑ i ∈ range n, a i) atTop (𝓝 y)
⊢ ∃ y, Tendsto (fun n => ∑ i ∈ range n, √(a i) / ↑n) atTop (𝓝 y)


/-- If $\Sigma a_{n}$ converges, and if $\left\{b_{n}\right\}$ is monotonic and bounded, prove that $\Sigma a_{n} b_{n}$ converges.-/

theorem exercise_3_8
  (a b : ℕ → ℝ)
  (h1 : ∃ y, (Tendsto (λ n => (∑ i in (range n), a i)) atTop (𝓝 y)))
  (h2 : Monotone b)
  (h3 : Bornology.IsBounded (Set.range b)) :
  ∃ y, Tendsto (λ n => (∑ i in (range n), (a i) * (b i))) atTop (𝓝 y) :=

a b : ℕ → ℝ
h1 : ∃ y, Tendsto (fun n => ∑ i ∈ range n, a i) atTop (𝓝 y)
h2 : Monotone b
h3 : Bornology.IsBounded (Set.range b)
⊢ ∃ y, Tendsto (fun n => ∑ i ∈ range n, a i * b i) atTop (𝓝 y)


/-- Prove that the Cauchy product of two absolutely convergent series converges absolutely.-/

theorem exercise_3_13
  (a b : ℕ → ℝ)
  (ha : ∃ y, (Tendsto (λ n => (∑ i in (range n), |a i|)) atTop (𝓝 y)))
  (hb : ∃ y, (Tendsto (λ n => (∑ i in (range n), |b i|)) atTop (𝓝 y))) :
  ∃ y, (Tendsto (λ n => (∑ i in (range n),
  λ i => (∑ j in range (i + 1), a j * b (i - j)))) atTop (𝓝 y)) :=

a b : ℕ → ℝ
ha : ∃ y, Tendsto (fun n => ∑ i ∈ range n, |a i|) atTop (𝓝 y)
hb : ∃ y, Tendsto (fun n => ∑ i ∈ range n, |b i|) atTop (𝓝 y)
⊢ ∃ y, Tendsto (fun n => ∑ i ∈ range n, fun i => ∑ j ∈ range (i + 1), a j * b (i - j)) atTop (𝓝 y)


/-- Suppose $\left\{p_{n}\right\}$ is a Cauchy sequence in a metric space $X$, and some sequence $\left\{p_{n l}\right\}$ converges to a point $p \in X$. Prove that the full sequence $\left\{p_{n}\right\}$ converges to $p$.-/

theorem exercise_3_20 {X : Type*} [MetricSpace X]
  (p : ℕ → X) (l : ℕ) (r : X)
  (hp : CauchySeq p)
  (hpl : Tendsto (λ n => p (l * n)) atTop (𝓝 r)) :
  Tendsto p atTop (𝓝 r) :=

X : Type u_1
inst✝ : MetricSpace X
p : ℕ → X
l : ℕ
r : X
hp : CauchySeq p
hpl : Tendsto (fun n => p (l * n)) atTop (𝓝 r)
⊢ Tendsto p atTop (𝓝 r)


/-- If $\left\{E_{n}\right\}$ is a sequence of closed nonempty and bounded sets in a complete metric space $X$, if $E_{n} \supset E_{n+1}$, and if $\lim _{n \rightarrow \infty} \operatorname{diam} E_{n}=0,$ then $\bigcap_{1}^{\infty} E_{n}$ consists of exactly one point.-/

theorem exercise_3_21
  {X : Type*} [MetricSpace X] [CompleteSpace X]
  (E : ℕ → Set X)
  (hE : ∀ n, E n ⊃ E (n + 1))
  (hE' : Tendsto (λ n => Metric.diam (E n)) atTop (𝓝 0)) :
  ∃ a, Set.iInter E = {a} :=

X : Type u_1
inst✝¹ : MetricSpace X
inst✝ : CompleteSpace X
E : ℕ → Set X
hE : ∀ (n : ℕ), E n ⊃ E (n + 1)
hE' : Tendsto (fun n => Metric.diam (E n)) atTop (𝓝 0)
⊢ ∃ a, Set.iInter E = {a}


/-- Suppose $X$ is a nonempty complete metric space, and $\left\{G_{n}\right\}$ is a sequence of dense open sets of $X$. Prove Baire's theorem, namely, that $\bigcap_{1}^{\infty} G_{n}$ is not empty.-/

theorem exercise_3_22 (X : Type*) [MetricSpace X] [CompleteSpace X]
  (G : ℕ → Set X) (hG : ∀ n, IsOpen (G n) ∧ Dense (G n)) :
  ∃ x, ∀ n, x ∈ G n :=

X : Type u_1
inst✝¹ : MetricSpace X
inst✝ : CompleteSpace X
G : ℕ → Set X
hG : ∀ (n : ℕ), IsOpen (G n) ∧ Dense (G n)
⊢ ∃ x, ∀ (n : ℕ), x ∈ G n


/-- Suppose $f$ is a real function defined on $\mathbb{R}$ which satisfies $\lim_{h \rightarrow 0} f(x + h) - f(x - h) = 0$ for every $x \in \mathbb{R}$. Show that $f$ does not need to be continuous.-/

theorem exercise_4_1a
  : ∃ (f : ℝ → ℝ), (∀ (x : ℝ), Tendsto (λ y => f (x + y) - f (x - y)) (𝓝 0) (𝓝 0)) ∧ ¬ Continuous f :=

⊢ ∃ f, (∀ (x : ℝ), Tendsto (fun y => f (x + y) - f (x - y)) (𝓝 0) (𝓝 0)) ∧ ¬Continuous f


/-- If $f$ is a continuous mapping of a metric space $X$ into a metric space $Y$, prove that $f(\overline{E}) \subset \overline{f(E)}$ for every set $E \subset X$. ($\overline{E}$ denotes the closure of $E$).-/

theorem exercise_4_2a
  {α : Type} [MetricSpace α]
  {β : Type} [MetricSpace β]
  (f : α → β)
  (h₁ : Continuous f)
  : ∀ (x : Set α), f '' (closure x) ⊆ closure (f '' x) :=

α : Type
inst✝¹ : MetricSpace α
β : Type
inst✝ : MetricSpace β
f : α → β
h₁ : Continuous f
⊢ ∀ (x : Set α), f '' closure x ⊆ closure (f '' x)


/-- Let $f$ be a continuous real function on a metric space $X$. Let $Z(f)$ (the zero set of $f$ ) be the set of all $p \in X$ at which $f(p)=0$. Prove that $Z(f)$ is closed.-/

theorem exercise_4_3
  {α : Type} [MetricSpace α]
  (f : α → ℝ) (h : Continuous f) (z : Set α) (g : z = f⁻¹' {0})
  : IsClosed z :=

α : Type
inst✝ : MetricSpace α
f : α → ℝ
h : Continuous f
z : Set α
g : z = f ⁻¹' {0}
⊢ IsClosed z


/-- Let $f$ and $g$ be continuous mappings of a metric space $X$ into a metric space $Y$, and let $E$ be a dense subset of $X$. Prove that $f(E)$ is dense in $f(X)$.-/

theorem exercise_4_4a
  {α : Type} [MetricSpace α]
  {β : Type} [MetricSpace β]
  (f : α → β)
  (s : Set α)
  (h₁ : Continuous f)
  (h₂ : Dense s)
  : f '' Set.univ ⊆ closure (f '' s) :=

α : Type
inst✝¹ : MetricSpace α
β : Type
inst✝ : MetricSpace β
f : α → β
s : Set α
h₁ : Continuous f
h₂ : Dense s
⊢ f '' Set.univ ⊆ closure (f '' s)


/-- Let $f$ and $g$ be continuous mappings of a metric space $X$ into a metric space $Y$, and let $E$ be a dense subset of $X$. Prove that if $g(p) = f(p)$ for all $p \in P$ then $g(p) = f(p)$ for all $p \in X$.-/

theorem exercise_4_4b
  {α : Type} [MetricSpace α]
  {β : Type} [MetricSpace β]
  (f g : α → β)
  (s : Set α)
  (h₁ : Continuous f)
  (h₂ : Continuous g)
  (h₃ : Dense s)
  (h₄ : ∀ x ∈ s, f x = g x)
  : f = g :=

α : Type
inst✝¹ : MetricSpace α
β : Type
inst✝ : MetricSpace β
f g : α → β
s : Set α
h₁ : Continuous f
h₂ : Continuous g
h₃ : Dense s
h₄ : ∀ x ∈ s, f x = g x
⊢ f = g


/-- If $f$ is a real continuous function defined on a closed set $E \subset \mathbb{R}$, prove that there exist continuous real functions $g$ on $\mathbb{R}$ such that $g(x)=f(x)$ for all $x \in E$.-/

theorem exercise_4_5a
  (f : ℝ → ℝ)
  (E : Set ℝ)
  (h₁ : IsClosed E)
  (h₂ : ContinuousOn f E)
  : ∃ (g : ℝ → ℝ), Continuous g ∧ ∀ x ∈ E, f x = g x :=

f : ℝ → ℝ
E : Set ℝ
h₁ : IsClosed E
h₂ : ContinuousOn f E
⊢ ∃ g, Continuous g ∧ ∀ x ∈ E, f x = g x


/-- Show that there exist a set $E \subset \mathbb{R}$ and a real continuous function $f$ defined on $E$, such that there does not exist a continuous real function $g$ on $\mathbb{R}$ such that $g(x)=f(x)$ for all $x \in E$.-/

theorem exercise_4_5b
  : ∃ (E : Set ℝ) (f : ℝ → ℝ), (ContinuousOn f E) ∧
  (¬ ∃ (g : ℝ → ℝ), Continuous g ∧ ∀ x ∈ E, f x = g x) :=

⊢ ∃ E f, ContinuousOn f E ∧ ¬∃ g, Continuous g ∧ ∀ x ∈ E, f x = g x


/-- If $f$ is defined on $E$, the graph of $f$ is the set of points $(x, f(x))$, for $x \in E$. In particular, if $E$ is a set of real numbers, and $f$ is real-valued, the graph of $f$ is a subset of the plane. Suppose $E$ is compact, and prove that $f$ is continuous on $E$ if and only if its graph is compact.-/

theorem exercise_4_6
  (f : ℝ → ℝ)
  (E : Set ℝ)
  (G : Set (ℝ × ℝ))
  (h₁ : IsCompact E)
  (h₂ : G = {(x, f x) | x ∈ E})
  : ContinuousOn f E ↔ IsCompact G :=

f : ℝ → ℝ
E : Set ℝ
G : Set (ℝ × ℝ)
h₁ : IsCompact E
h₂ : G = {x | ∃ x_1 ∈ E, (x_1, f x_1) = x}
⊢ ContinuousOn f E ↔ IsCompact G


/-- Let $f$ be a real uniformly continuous function on the bounded set $E$ in $R^{1}$. Prove that $f$ is bounded on $E$.-/

theorem exercise_4_8a
  (E : Set ℝ) (f : ℝ → ℝ) (hf : UniformContinuousOn f E)
  (hE : Bornology.IsBounded E) : Bornology.IsBounded (Set.image f E) :=

E : Set ℝ
f : ℝ → ℝ
hf : UniformContinuousOn f E
hE : Bornology.IsBounded E
⊢ Bornology.IsBounded (f '' E)


/-- Let $E$ be a bounded set in $R^{1}$. Prove that there exists a real function $f$ such that $f$ is uniformly continuous and is not bounded on $E$.-/

theorem exercise_4_8b
  (E : Set ℝ) :
  ∃ f : ℝ → ℝ, UniformContinuousOn f E ∧ ¬ Bornology.IsBounded (Set.image f E) :=

E : Set ℝ
⊢ ∃ f, UniformContinuousOn f E ∧ ¬Bornology.IsBounded (f '' E)


/-- Suppose $f$ is a uniformly continuous mapping of a metric space $X$ into a metric space $Y$ and prove that $\left\{f\left(x_{n}\right)\right\}$ is a Cauchy sequence in $Y$ for every Cauchy sequence $\{x_n\}$ in $X$.-/

theorem exercise_4_11a
  {X : Type*} [MetricSpace X]
  {Y : Type*} [MetricSpace Y]
  (f : X → Y) (hf : UniformContinuous f)
  (x : ℕ → X) (hx : CauchySeq x) :
  CauchySeq (λ n => f (x n)) :=

X : Type u_1
inst✝¹ : MetricSpace X
Y : Type u_2
inst✝ : MetricSpace Y
f : X → Y
hf : UniformContinuous f
x : ℕ → X
hx : CauchySeq x
⊢ CauchySeq fun n => f (x n)


/-- A uniformly continuous function of a uniformly continuous function is uniformly continuous.-/

theorem exercise_4_12
  {α β γ : Type*} [UniformSpace α] [UniformSpace β] [UniformSpace γ]
  {f : α → β} {g : β → γ}
  (hf : UniformContinuous f) (hg : UniformContinuous g) :
  UniformContinuous (g ∘ f) :=

α : Type u_1
β : Type u_2
γ : Type u_3
inst✝² : UniformSpace α
inst✝¹ : UniformSpace β
inst✝ : UniformSpace γ
f : α → β
g : β → γ
hf : UniformContinuous f
hg : UniformContinuous g
⊢ UniformContinuous (g ∘ f)


/-- Prove that every continuous open mapping of $R^{1}$ into $R^{1}$ is monotonic.-/

theorem exercise_4_15 {f : ℝ → ℝ}
  (hf : Continuous f) (hof : IsOpenMap f) :
  Monotone f :=

f : ℝ → ℝ
hf : Continuous f
hof : IsOpenMap f
⊢ Monotone f


/-- Suppose $f$ is a real function with domain $R^{1}$ which has the intermediate value property: if $f(a)<c<f(b)$, then $f(x)=c$ for some $x$ between $a$ and $b$. Suppose also, for every rational $r$, that the set of all $x$ with $f(x)=r$ is closed. Prove that $f$ is continuous.-/

theorem exercise_4_19
  {f : ℝ → ℝ} (hf : ∀ a b c, a < b → f a < c → c < f b → ∃ x, a < x ∧ x < b ∧ f x = c)
  (hg : ∀ r : ℚ, IsClosed {x | f x = r}) : Continuous f :=

f : ℝ → ℝ
hf : ∀ (a b c : ℝ), a < b → f a < c → c < f b → ∃ x, a < x ∧ x < b ∧ f x = c
hg : ∀ (r : ℚ), IsClosed {x | f x = ↑r}
⊢ Continuous f


/-- Suppose $K$ and $F$ are disjoint sets in a metric space $X, K$ is compact, $F$ is closed. Prove that there exists $\delta>0$ such that $d(p, q)>\delta$ if $p \in K, q \in F$.-/

theorem exercise_4_21a {X : Type*} [MetricSpace X]
  (K F : Set X) (hK : IsCompact K) (hF : IsClosed F) (hKF : Disjoint K F) :
  ∃ (δ : ℝ), δ > 0 ∧ ∀ (p q : X), p ∈ K → q ∈ F → dist p q ≥ δ :=

X : Type u_1
inst✝ : MetricSpace X
K F : Set X
hK : IsCompact K
hF : IsClosed F
hKF : Disjoint K F
⊢ ∃ δ > 0, ∀ (p q : X), p ∈ K → q ∈ F → dist p q ≥ δ


/-- Assume that $f$ is a continuous real function defined in $(a, b)$ such that $f\left(\frac{x+y}{2}\right) \leq \frac{f(x)+f(y)}{2}$ for all $x, y \in(a, b)$. Prove that $f$ is convex.-/

theorem exercise_4_24 {f : ℝ → ℝ}
  (hf : Continuous f) (a b : ℝ) (hab : a < b)
  (h : ∀ x y : ℝ, a < x → x < b → a < y → y < b → f ((x + y) / 2) ≤ (f x + f y) / 2) :
  ConvexOn ℝ (Set.Ioo a b) f :=

f : ℝ → ℝ
hf : Continuous f
a b : ℝ
hab : a < b
h : ∀ (x y : ℝ), a < x → x < b → a < y → y < b → f ((x + y) / 2) ≤ (f x + f y) / 2
⊢ ConvexOn ℝ (Set.Ioo a b) f


/-- Let $f$ be defined for all real $x$, and suppose that $|f(x)-f(y)| \leq (x-y)^{2}$ for all real $x$ and $y$. Prove that $f$ is constant.-/

theorem exercise_5_1
  {f : ℝ → ℝ} (hf : ∀ x y : ℝ, |(f x - f y)| ≤ (x - y) ^ 2) :
  ∃ c, f = λ x => c :=

f : ℝ → ℝ
hf : ∀ (x y : ℝ), |f x - f y| ≤ (x - y) ^ 2
⊢ ∃ c, f = fun x => c


/-- Suppose $f^{\prime}(x)>0$ in $(a, b)$. Prove that $f$ is strictly increasing in $(a, b)$, and let $g$ be its inverse function. Prove that $g$ is differentiable, and that $g^{\prime}(f(x))=\frac{1}{f^{\prime}(x)} \quad(a<x<b)$.-/

theorem exercise_5_2 {a b : ℝ}
  {f g : ℝ → ℝ} (hf : ∀ x ∈ Set.Ioo a b, deriv f x > 0)
  (hg : g = f⁻¹)
  (hg_diff : DifferentiableOn ℝ g (Set.Ioo a b)) :
  DifferentiableOn ℝ g (Set.Ioo a b) ∧
  ∀ x ∈ Set.Ioo a b, deriv g x = 1 / deriv f x :=

a b : ℝ
f g : ℝ → ℝ
hf : ∀ x ∈ Set.Ioo a b, deriv f x > 0
hg : g = f⁻¹
hg_diff : DifferentiableOn ℝ g (Set.Ioo a b)
⊢ DifferentiableOn ℝ g (Set.Ioo a b) ∧ ∀ x ∈ Set.Ioo a b, deriv g x = 1 / deriv f x


/-- Suppose $g$ is a real function on $R^{1}$, with bounded derivative (say $\left|g^{\prime}\right| \leq M$ ). Fix $\varepsilon>0$, and define $f(x)=x+\varepsilon g(x)$. Prove that $f$ is one-to-one if $\varepsilon$ is small enough.-/

theorem exercise_5_3 {g : ℝ → ℝ} (hg : Continuous g)
  (hg' : ∃ M : ℝ, ∀ x : ℝ, |deriv g x| ≤ M) :
  ∃ N, ∀ ε > 0, ε < N → Function.Injective (λ x : ℝ => x + ε * g x) :=

g : ℝ → ℝ
hg : Continuous g
hg' : ∃ M, ∀ (x : ℝ), |deriv g x| ≤ M
⊢ ∃ N, ∀ ε > 0, ε < N → Function.Injective fun x => x + ε * g x


/-- If $C_{0}+\frac{C_{1}}{2}+\cdots+\frac{C_{n-1}}{n}+\frac{C_{n}}{n+1}=0,$ where $C_{0}, \ldots, C_{n}$ are real constants, prove that the equation $C_{0}+C_{1} x+\cdots+C_{n-1} x^{n-1}+C_{n} x^{n}=0$ has at least one real root between 0 and 1.-/

theorem exercise_5_4 {n : ℕ}
  (C : ℕ → ℝ)
  (hC : ∑ i in (range (n + 1)), (C i) / (i + 1) = 0) :
  ∃ x, x ∈ (Set.Icc (0 : ℝ) 1) ∧ ∑ i in range (n + 1), (C i) * (x^i) = 0 :=

n : ℕ
C : ℕ → ℝ
hC : ∑ i ∈ range (n + 1), C i / (↑i + 1) = 0
⊢ ∃ x ∈ Set.Icc 0 1, ∑ i ∈ range (n + 1), C i * x ^ i = 0


/-- Suppose $f$ is defined and differentiable for every $x>0$, and $f^{\prime}(x) \rightarrow 0$ as $x \rightarrow+\infty$. Put $g(x)=f(x+1)-f(x)$. Prove that $g(x) \rightarrow 0$ as $x \rightarrow+\infty$.-/

theorem exercise_5_5
  {f : ℝ → ℝ}
  (hfd : Differentiable ℝ f)
  (hf : Tendsto (deriv f) atTop (𝓝 0)) :
  Tendsto (λ x => f (x + 1) - f x) atTop atTop :=

f : ℝ → ℝ
hfd : Differentiable ℝ f
hf : Tendsto (deriv f) atTop (𝓝 0)
⊢ Tendsto (fun x => f (x + 1) - f x) atTop atTop


/-- Suppose (a) $f$ is continuous for $x \geq 0$, (b) $f^{\prime}(x)$ exists for $x>0$, (c) $f(0)=0$, (d) $f^{\prime}$ is monotonically increasing. Put $g(x)=\frac{f(x)}{x} \quad(x>0)$ and prove that $g$ is monotonically increasing.-/

theorem exercise_5_6
  {f : ℝ → ℝ}
  (hf1 : Continuous f)
  (hf2 : ∀ x, DifferentiableAt ℝ f x)
  (hf3 : f 0 = 0)
  (hf4 : Monotone (deriv f)) :
  MonotoneOn (λ x => f x / x) (Set.Ioi 0) :=

f : ℝ → ℝ
hf1 : Continuous f
hf2 : ∀ (x : ℝ), DifferentiableAt ℝ f x
hf3 : f 0 = 0
hf4 : Monotone (deriv f)
⊢ MonotoneOn (fun x => f x / x) (Set.Ioi 0)


/-- Suppose $f^{\prime}(x), g^{\prime}(x)$ exist, $g^{\prime}(x) \neq 0$, and $f(x)=g(x)=0$. Prove that $\lim _{t \rightarrow x} \frac{f(t)}{g(t)}=\frac{f^{\prime}(x)}{g^{\prime}(x)}.$-/

theorem exercise_5_7
  {f g : ℝ → ℝ} {x : ℝ}
  (hf' : DifferentiableAt ℝ f 0)
  (hg' : DifferentiableAt ℝ g 0)
  (hg'_ne_0 : deriv g 0 ≠ 0)
  (f0 : f 0 = 0) (g0 : g 0 = 0) :
  Tendsto (λ x => f x / g x) (𝓝 x) (𝓝 (deriv f x / deriv g x)) :=

f g : ℝ → ℝ
x : ℝ
hf' : DifferentiableAt ℝ f 0
hg' : DifferentiableAt ℝ g 0
hg'_ne_0 : deriv g 0 ≠ 0
f0 : f 0 = 0
g0 : g 0 = 0
⊢ Tendsto (fun x => f x / g x) (𝓝 x) (𝓝 (deriv f x / deriv g x))


/-- Suppose $a \in R^{1}, f$ is a twice-differentiable real function on $(a, \infty)$, and $M_{0}, M_{1}, M_{2}$ are the least upper bounds of $|f(x)|,\left|f^{\prime}(x)\right|,\left|f^{\prime \prime}(x)\right|$, respectively, on $(a, \infty)$. Prove that $M_{1}^{2} \leq 4 M_{0} M_{2} .$-/

theorem exercise_5_15 {f : ℝ → ℝ} (a M0 M1 M2 : ℝ)
  (hf' : DifferentiableOn ℝ f (Set.Ici a))
  (hf'' : DifferentiableOn ℝ (deriv f) (Set.Ici a))
  (hM0 : M0 = sSup {(|f x|) | x ∈ (Set.Ici a)})
  (hM1 : M1 = sSup {(|deriv f x|) | x ∈ (Set.Ici a)})
  (hM2 : M2 = sSup {(|deriv (deriv f) x|) | x ∈ (Set.Ici a)}) :
  (M1 ^ 2) ≤ 4 * M0 * M2 :=

f : ℝ → ℝ
a M0 M1 M2 : ℝ
hf' : DifferentiableOn ℝ f (Set.Ici a)
hf'' : DifferentiableOn ℝ (deriv f) (Set.Ici a)
hM0 : M0 = sSup {x | ∃ x_1 ∈ Set.Ici a, |f x_1| = x}
hM1 : M1 = sSup {x | ∃ x_1 ∈ Set.Ici a, |deriv f x_1| = x}
hM2 : M2 = sSup {x | ∃ x_1 ∈ Set.Ici a, |deriv (deriv f) x_1| = x}
⊢ M1 ^ 2 ≤ 4 * M0 * M2


/-- Suppose $f$ is a real, three times differentiable function on $[-1,1]$, such that $f(-1)=0, \quad f(0)=0, \quad f(1)=1, \quad f^{\prime}(0)=0 .$ Prove that $f^{(3)}(x) \geq 3$ for some $x \in(-1,1)$.-/

theorem exercise_5_17
  {f : ℝ → ℝ}
  (hf' : DifferentiableOn ℝ f (Set.Icc (-1) 1))
  (hf'' : DifferentiableOn ℝ (deriv f) (Set.Icc 1 1))
  (hf''' : DifferentiableOn ℝ (deriv (deriv f)) (Set.Icc 1 1))
  (hf0 : f (-1) = 0)
  (hf1 : f 0 = 0)
  (hf2 : f 1 = 1)
  (hf3 : deriv f 0 = 0) :
  ∃ x, x ∈ Set.Ioo (-1 : ℝ) 1 ∧ deriv (deriv (deriv f)) x ≥ 3 :=

f : ℝ → ℝ
hf' : DifferentiableOn ℝ f (Set.Icc (-1) 1)
hf'' : DifferentiableOn ℝ (deriv f) (Set.Icc 1 1)
hf''' : DifferentiableOn ℝ (deriv (deriv f)) (Set.Icc 1 1)
hf0 : f (-1) = 0
hf1 : f 0 = 0
hf2 : f 1 = 1
hf3 : deriv f 0 = 0
⊢ ∃ x ∈ Set.Ioo (-1) 1, deriv (deriv (deriv f)) x ≥ 3


/-- If $G$ is a finite group of even order, show that there must be an element $a \neq e$ such that $a=a^{-1}$.-/

theorem exercise_2_1_18 {G : Type*} [Group G]
  [Fintype G] (hG2 : Even (card G)) :
  ∃ (a : G), a ≠ 1 ∧ a = a⁻¹ :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG2 : Even (card G)
⊢ ∃ a, a ≠ 1 ∧ a = a⁻¹

import Mathlib

open Fintype Set Real Ideal Polynomial
open scoped BigOperators


/-- Show that a group of order 5 must be abelian.-/

def exercise_2_1_21 (G : Type*) [Group G] [Fintype G]
  (hG : card G = 5) :
  CommGroup G :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 5
⊢ CommGroup G


/-- If $G$ is a finite group, prove that, given $a \in G$, there is a positive integer $n$, depending on $a$, such that $a^n = e$.-/

theorem exercise_2_1_26 {G : Type*} [Group G]
  [Fintype G] (a : G) : ∃ (n : ℕ), a ^ n = 1 :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
a : G
⊢ ∃ n, a ^ n = 1


/-- If $G$ is a finite group, prove that there is an integer $m > 0$ such that $a^m = e$ for all $a \in G$.-/

theorem exercise_2_1_27 {G : Type*} [Group G]
  [Fintype G] : ∃ (m : ℕ), ∀ (a : G), a ^ m = 1 :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
⊢ ∃ m, ∀ (a : G), a ^ m = 1


/-- If $G$ is a group in which $(a b)^{i}=a^{i} b^{i}$ for three consecutive integers $i$, prove that $G$ is abelian.-/

def exercise_2_2_3 {G : Type*} [Group G]
  {P : ℕ → Prop} {hP : P = λ i => ∀ a b : G, (a*b)^i = a^i * b^i}
  (hP1 : ∃ n : ℕ, P n ∧ P (n+1) ∧ P (n+2)) : CommGroup G :=

G : Type u_1
inst✝ : Group G
P : ℕ → Prop
hP : P = fun i => ∀ (a b : G), (a * b) ^ i = a ^ i * b ^ i
hP1 : ∃ n, P n ∧ P (n + 1) ∧ P (n + 2)
⊢ CommGroup G


/-- Let $G$ be a group in which $(a b)^{3}=a^{3} b^{3}$ and $(a b)^{5}=a^{5} b^{5}$ for all $a, b \in G$. Show that $G$ is abelian.-/

def exercise_2_2_5 {G : Type*} [Group G]
  (h : ∀ (a b : G), (a * b) ^ 3 = a ^ 3 * b ^ 3 ∧ (a * b) ^ 5 = a ^ 5 * b ^ 5) :
  CommGroup G :=

G : Type u_1
inst✝ : Group G
h : ∀ (a b : G), (a * b) ^ 3 = a ^ 3 * b ^ 3 ∧ (a * b) ^ 5 = a ^ 5 * b ^ 5
⊢ CommGroup G


/-- Let $G$ be a group in which $(a b)^{n}=a^{n} b^{n}$ for some fixed integer $n>1$ for all $a, b \in G$. For all $a, b \in G$, prove that $\left(a b a^{-1} b^{-1}\right)^{n(n-1)}=e$.-/

theorem exercise_2_2_6c {G : Type*} [Group G] {n : ℕ} (hn : n > 1)
  (h : ∀ (a b : G), (a * b) ^ n = a ^ n * b ^ n) :
  ∀ (a b : G), (a * b * a⁻¹ * b⁻¹) ^ (n * (n - 1)) = 1 :=

G : Type u_1
inst✝ : Group G
n : ℕ
hn : n > 1
h : ∀ (a b : G), (a * b) ^ n = a ^ n * b ^ n
⊢ ∀ (a b : G), (a * b * a⁻¹ * b⁻¹) ^ (n * (n - 1)) = 1


/-- If $G$ is a group and $a, x \in G$, prove that $C\left(x^{-1} a x\right)=x^{-1} C(a) x$-/

theorem exercise_2_3_17 {G : Type*} [Mul G] [Group G] (a x : G) :
  centralizer {x⁻¹*a*x} =
  (λ g : G => x⁻¹*g*x) '' (centralizer {a}) :=

G : Type u_1
inst✝¹ : Mul G
inst✝ : Group G
a x : G
⊢ {x⁻¹ * a * x}.centralizer = (fun g => x⁻¹ * g * x) '' {a}.centralizer


/-- If a group $G$ has no proper subgroups, prove that $G$ is cyclic of order $p$, where $p$ is a prime number.-/

theorem exercise_2_3_16 {G : Type*} [Group G]
  (hG : ∀ H : Subgroup G, H = ⊤ ∨ H = ⊥) :
  IsCyclic G ∧ ∃ (p : ℕ) (Fin : Fintype G), Nat.Prime p ∧ @card G Fin = p :=

G : Type u_1
inst✝ : Group G
hG : ∀ (H : Subgroup G), H = ⊤ ∨ H = ⊥
⊢ IsCyclic G ∧ ∃ p Fin, p.Prime ∧ card G = p


/-- If $a > 1$ is an integer, show that $n \mid \varphi(a^n - 1)$, where $\phi$ is the Euler $\varphi$-function.-/

theorem exercise_2_4_36 {a n : ℕ} (h : a > 1) :
  n ∣ (a ^ n - 1).totient :=

a n : ℕ
h : a > 1
⊢ n ∣ (a ^ n - 1).totient


/-- Let $G$ be a group such that all subgroups of $G$ are normal in $G$. If $a, b \in G$, prove that $ba = a^jb$ for some $j$.-/

theorem exercise_2_5_23 {G : Type*} [Group G]
  (hG : ∀ (H : Subgroup G), H.Normal) (a b : G) :
  ∃ (j : ℤ) , b*a = a^j * b :=

G : Type u_1
inst✝ : Group G
hG : ∀ (H : Subgroup G), H.Normal
a b : G
⊢ ∃ j, b * a = a ^ j * b


/-- Suppose that $|G| = pm$, where $p \nmid m$ and $p$ is a prime. If $H$ is a normal subgroup of order $p$ in $G$, prove that $H$ is characteristic.-/

theorem exercise_2_5_30 {G : Type*} [Group G] [Fintype G]
  {p m : ℕ} (hp : Nat.Prime p) (hp1 : ¬ p ∣ m) (hG : card G = p*m)
  {H : Subgroup G} [Fintype H] [H.Normal] (hH : card H = p):
  Subgroup.Characteristic H :=

G : Type u_1
inst✝³ : Group G
inst✝² : Fintype G
p m : ℕ
hp : p.Prime
hp1 : ¬p ∣ m
hG : card G = p * m
H : Subgroup G
inst✝¹ : Fintype ↥H
inst✝ : H.Normal
hH : card ↥H = p
⊢ H.Characteristic


/-- Suppose that $G$ is an abelian group of order $p^nm$ where $p \nmid m$ is a prime.  If $H$ is a subgroup of $G$ of order $p^n$, prove that $H$ is a characteristic subgroup of $G$.-/

theorem exercise_2_5_31 {G : Type*} [CommGroup G] [Fintype G]
  {p m n : ℕ} (hp : Nat.Prime p) (hp1 : ¬ p ∣ m) (hG : card G = p^n*m)
  {H : Subgroup G} [Fintype H] (hH : card H = p^n) :
  Subgroup.Characteristic H :=

G : Type u_1
inst✝² : CommGroup G
inst✝¹ : Fintype G
p m n : ℕ
hp : p.Prime
hp1 : ¬p ∣ m
hG : card G = p ^ n * m
H : Subgroup G
inst✝ : Fintype ↥H
hH : card ↥H = p ^ n
⊢ H.Characteristic


/-- If $G$ is a nonabelian group of order 6, prove that $G \simeq S_3$.-/

def exercise_2_5_37 (G : Type*) [Group G] [Fintype G]
  (hG : card G = 6) (hG' : IsEmpty (CommGroup G)) :
  G ≃* Equiv.Perm (Fin 3) :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 6
hG' : IsEmpty (CommGroup G)
⊢ G ≃* Equiv.Perm (Fin 3)


/-- Prove that a group of order 9 must be abelian.-/

def exercise_2_5_43 (G : Type*) [Group G] [Fintype G]
  (hG : card G = 9) :
  CommGroup G :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 9
⊢ CommGroup G


/-- Prove that a group of order $p^2$, $p$ a prime, has a normal subgroup of order $p$.-/

theorem exercise_2_5_44 {G : Type*} [Group G] [Fintype G] {p : ℕ}
  (hp : Nat.Prime p) (hG : card G = p^2) :
  ∃ (N : Subgroup G) (Fin : Fintype N), @card N Fin = p ∧ N.Normal :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
p : ℕ
hp : p.Prime
hG : card G = p ^ 2
⊢ ∃ N Fin, card ↥N = p ∧ N.Normal


/-- Let $G$ be a finite group and $\varphi$ an automorphism of $G$ such that $\varphi(x) = x^{-1}$ for more than three-fourths of the elements of $G$. Prove that $\varphi(y) = y^{-1}$ for all $y \in G$, and so $G$ is abelian.-/

theorem exercise_2_5_52 {G : Type*} [Group G] [Fintype G]
  (φ : G ≃* G) {I : Finset G} (hI : ∀ x ∈ I, φ x = x⁻¹)
  (hI1 : (0.75 : ℚ) * card G ≤ card I) :
  ∀ x : G, φ x = x⁻¹ ∧ ∀ x y : G, x*y = y*x :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
φ : G ≃* G
I : Finset G
hI : ∀ x ∈ I, φ x = x⁻¹
hI1 : 0.75 * ↑(card G) ≤ ↑(card { x // x ∈ I })
⊢ ∀ (x : G), φ x = x⁻¹ ∧ ∀ (x y : G), x * y = y * x


/-- If $G$ is an abelian group and if $G$ has an element of order $m$ and one of order $n$, where $m$ and $n$ are relatively prime, prove that $G$ has an element of order $mn$.-/

theorem exercise_2_6_15 {G : Type*} [CommGroup G] {m n : ℕ}
  (hm : ∃ (g : G), orderOf g = m)
  (hn : ∃ (g : G), orderOf g = n)
  (hmn : m.Coprime n) :
  ∃ (g : G), orderOf g = m * n :=

G : Type u_1
inst✝ : CommGroup G
m n : ℕ
hm : ∃ g, orderOf g = m
hn : ∃ g, orderOf g = n
hmn : m.Coprime n
⊢ ∃ g, orderOf g = m * n


/-- If $\varphi$ is a homomorphism of $G$ onto $G'$ and $N \triangleleft G$, show that $\varphi(N) \triangleleft G'$.-/

theorem exercise_2_7_7 {G : Type*} [Group G] {G' : Type*} [Group G']
  (φ : G →* G') (N : Subgroup G) [N.Normal] :
  (Subgroup.map φ N).Normal :=

G : Type u_1
inst✝² : Group G
G' : Type u_2
inst✝¹ : Group G'
φ : G →* G'
N : Subgroup G
inst✝ : N.Normal
⊢ (Subgroup.map φ N).Normal


/-- Prove that any two nonabelian groups of order 21 are isomorphic.-/

def exercise_2_8_12 {G H : Type*} [Fintype G] [Fintype H]
  [Group G] [Group H] (hG : card G = 21) (hH : card H = 21)
  (hG1 : IsEmpty (CommGroup G)) (hH1 : IsEmpty (CommGroup H)) :
  G ≃* H :=

G : Type u_1
H : Type u_2
inst✝³ : Fintype G
inst✝² : Fintype H
inst✝¹ : Group G
inst✝ : Group H
hG : card G = 21
hH : card H = 21
hG1 : IsEmpty (CommGroup G)
hH1 : IsEmpty (CommGroup H)
⊢ G ≃* H


/-- Prove that if $p > q$ are two primes such that $q \mid p - 1$, then any two nonabelian groups of order $pq$ are isomorphic.-/

def exercise_2_8_15 {G H: Type*} [Fintype G] [Group G] [Fintype H]
  [Group H] {p q : ℕ} (hp : Nat.Prime p) (hq : Nat.Prime q)
  (h : p > q) (h1 : q ∣ p - 1) (hG : card G = p*q) (hH : card G = p*q) :
  G ≃* H :=

G : Type u_1
H : Type u_2
inst✝³ : Fintype G
inst✝² : Group G
inst✝¹ : Fintype H
inst✝ : Group H
p q : ℕ
hp : p.Prime
hq : q.Prime
h : p > q
h1 : q ∣ p - 1
hG hH : card G = p * q
⊢ G ≃* H


/-- If $G_1$ and $G_2$ are cyclic groups of orders $m$ and $n$, respectively, prove that $G_1 \times G_2$ is cyclic if and only if $m$ and $n$ are relatively prime.-/

theorem exercise_2_9_2 {G H : Type*} [Fintype G] [Fintype H] [Group G]
  [Group H] (hG : IsCyclic G) (hH : IsCyclic H) :
  IsCyclic (G × H) ↔ (card G).Coprime (card H) :=

G : Type u_1
H : Type u_2
inst✝³ : Fintype G
inst✝² : Fintype H
inst✝¹ : Group G
inst✝ : Group H
hG : IsCyclic G
hH : IsCyclic H
⊢ IsCyclic (G × H) ↔ (card G).Coprime (card H)


/-- Let $A$ be a normal subgroup of a group $G$, and suppose that $b \in G$ is an element of prime order $p$, and that $b \not\in A$. Show that $A \cap (b) = (e)$.-/

theorem exercise_2_10_1 {G : Type*} [Group G] (A : Subgroup G)
  [A.Normal] {b : G} (hp : Nat.Prime (orderOf b)) :
  A ⊓ (Subgroup.closure {b}) = ⊥ :=

G : Type u_1
inst✝¹ : Group G
A : Subgroup G
inst✝ : A.Normal
b : G
hp : (orderOf b).Prime
⊢ A ⊓ Subgroup.closure {b} = ⊥


/-- If $P$ is a $p$-Sylow subgroup of $G$ and $P \triangleleft G$, prove that $P$ is the only $p$-Sylow subgroup of $G$.-/

theorem exercise_2_11_6 {G : Type*} [Group G] {p : ℕ} (hp : Nat.Prime p)
  {P : Sylow p G} (hP : P.Normal) :
  ∀ (Q : Sylow p G), P = Q :=

G : Type u_1
inst✝ : Group G
p : ℕ
hp : p.Prime
P : Sylow p G
hP : (↑P).Normal
⊢ ∀ (Q : Sylow p G), P = Q


/-- If $P \triangleleft G$, $P$ a $p$-Sylow subgroup of $G$, prove that $\varphi(P) = P$ for every automorphism $\varphi$ of $G$.-/

theorem exercise_2_11_7 {G : Type*} [Group G] {p : ℕ} (hp : Nat.Prime p)
  {P : Sylow p G} (hP : P.Normal) :
  Subgroup.Characteristic (P : Subgroup G) :=

G : Type u_1
inst✝ : Group G
p : ℕ
hp : p.Prime
P : Sylow p G
hP : (↑P).Normal
⊢ (↑P).Characteristic


/-- Show that any subgroup of order $p^{n-1}$ in a group $G$ of order $p^n$ is normal in $G$.-/

theorem exercise_2_11_22 {p : ℕ} {n : ℕ} {G : Type*} [Fintype G]
  [Group G] (hp : Nat.Prime p) (hG : card G = p ^ n) {K : Subgroup G}
  [Fintype K] (hK : card K = p ^ (n-1)) :
  K.Normal :=

p n : ℕ
G : Type u_1
inst✝² : Fintype G
inst✝¹ : Group G
hp : p.Prime
hG : card G = p ^ n
K : Subgroup G
inst✝ : Fintype ↥K
hK : card ↥K = p ^ (n - 1)
⊢ K.Normal


/-- If $\sigma, \tau$ are two permutations that disturb no common element and $\sigma \tau = e$, prove that $\sigma = \tau = e$.-/

theorem exercise_3_2_21 {α : Type*} [Fintype α] {σ τ: Equiv.Perm α}
  (h1 : ∀ a : α, σ a = a ↔ τ a ≠ a) (h2 : τ ∘ σ = id) :
  σ = 1 ∧ τ = 1 :=

α : Type u_1
inst✝ : Fintype α
σ τ : Equiv.Perm α
h1 : ∀ (a : α), σ a = a ↔ τ a ≠ a
h2 : ⇑τ ∘ ⇑σ = id
⊢ σ = 1 ∧ τ = 1


/-- Show that there is an infinite number of solutions to $x^2 = -1$ in the quaternions.-/

theorem exercise_4_1_19 : Infinite {x : Quaternion ℝ | x^2 = -1} :=


/-- Let $T$ be the group of $2\times 2$ matrices $A$ with entries in the field $\mathbb{Z}_2$ such that $\det A$ is not equal to 0. Prove that $T$ is isomorphic to $S_3$, the symmetric group of degree 3.-/

def exercise_4_1_34 : Equiv.Perm (Fin 3) ≃* Matrix.GeneralLinearGroup (Fin 2) (ZMod 2) :=

⊢ Equiv.Perm (Fin 3) ≃* GL (Fin 2) (ZMod 2)


/-- Let $R$ be a ring in which $x^3 = x$ for every $x \in R$. Prove that $R$ is commutative.-/

def exercise_4_2_5 {R : Type*} [Ring R]
  (h : ∀ x : R, x ^ 3 = x) : CommRing R :=

R : Type u_1
inst✝ : Ring R
h : ∀ (x : R), x ^ 3 = x
⊢ CommRing R


/-- If $a^2 = 0$ in $R$, show that $ax + xa$ commutes with $a$.-/

theorem exercise_4_2_6 {R : Type*} [Ring R] (a x : R)
  (h : a ^ 2 = 0) : a * (a * x + x * a) = (x + x * a) * a :=

R : Type u_1
inst✝ : Ring R
a x : R
h : a ^ 2 = 0
⊢ a * (a * x + x * a) = (x + x * a) * a


/-- Let $p$ be an odd prime and let $1 + \frac{1}{2} + ... + \frac{1}{p - 1} = \frac{a}{b}$, where $a, b$ are integers. Show that $p \mid a$.-/

theorem exercise_4_2_9 {p : ℕ} (hp : Nat.Prime p) (hp1 : Odd p) :
  ∃ (a b : ℤ), (a / b : ℚ) = ∑ i in Finset.range p, 1 / (i + 1) → ↑p ∣ a :=

p : ℕ
hp : p.Prime
hp1 : Odd p
⊢ ∃ a b, ↑a / ↑b = ↑(∑ i ∈ Finset.range p, 1 / (i + 1)) → ↑p ∣ a


/-- If $R$ is a commutative ring and $a \in R$, let $L(a) = \{x \in R \mid xa = 0\}$. Prove that $L(a)$ is an ideal of $R$.-/

theorem exercise_4_3_1 {R : Type*} [CommRing R] (a : R) :
  ∃ I : Ideal R, {x : R | x*a=0} = I :=

R : Type u_1
inst✝ : CommRing R
a : R
⊢ ∃ I, {x | x * a = 0} = ↑I


/-- Let $R$ be the ring of $2 \times 2$ matrices over the real numbers; suppose that $I$ is an ideal of $R$. Show that $I = (0)$ or $I = R$.-/

theorem exercise_4_3_25 (I : Ideal (Matrix (Fin 2) (Fin 2) ℝ)) :
  I = ⊥ ∨ I = ⊤ :=

I : Ideal (Matrix (Fin 2) (Fin 2) ℝ)
⊢ I = ⊥ ∨ I = ⊤


/-- Show that $(p - 1)/2$ of the numbers $1, 2, \ldots, p - 1$ are quadratic residues and $(p - 1)/2$ are quadratic nonresidues $\mod p$.-/

theorem exercise_4_4_9 (p : ℕ) (hp : Nat.Prime p) :
  (∃ S : Finset (ZMod p), S.card = (p-1)/2 ∧ ∃ x : ZMod p, x^2 = p) ∧
  (∃ S : Finset (ZMod p), S.card = (p-1)/2 ∧ ¬ ∃ x : ZMod p, x^2 = p) :=

p : ℕ
hp : p.Prime
⊢ (∃ S, S.card = (p - 1) / 2 ∧ ∃ x, x ^ 2 = ↑p) ∧ ∃ S, S.card = (p - 1) / 2 ∧ ¬∃ x, x ^ 2 = ↑p


/-- Let $F = \mathbb{Z}_p$ be the field of integers $\mod p$, where $p$ is a prime, and let $q(x) \in F[x]$ be irreducible of degree $n$. Show that $F[x]/(q(x))$ is a field having at exactly $p^n$ elements.-/

theorem exercise_4_5_16 {p n: ℕ} (hp : Nat.Prime p)
  {q : Polynomial (ZMod p)} (hq : Irreducible q) (hn : q.degree = n) :
  ∃ is_fin : Fintype $ Polynomial (ZMod p) ⧸ span ({q} : Set (Polynomial $ ZMod p)),
  @card (Polynomial (ZMod p) ⧸ span {q}) is_fin = p ^ n ∧
  IsField (Polynomial $ ZMod p) :=

p n : ℕ
hp : p.Prime
q : (ZMod p)[X]
hq : Irreducible q
hn : q.degree = ↑n
⊢ ∃ is_fin, card ((ZMod p)[X] ⧸ span {q}) = p ^ n ∧ IsField (ZMod p)[X]


/-- Let $F = \mathbb{Z}_7$ and let $p(x) = x^3 - 2$ and $q(x) = x^3 + 2$ be in $F[x]$. Show that $p(x)$ and $q(x)$ are irreducible in $F[x]$ and that the fields $F[x]/(p(x))$ and $F[x]/(q(x))$ are isomorphic.-/

theorem exercise_4_5_23 {p q: Polynomial (ZMod 7)}
  (hp : p = X^3 - 2) (hq : q = X^3 + 2) :
  Irreducible p ∧ Irreducible q ∧
  (Nonempty $ Polynomial (ZMod 7) ⧸ span ({p} : Set $ Polynomial $ ZMod 7) ≃+*
  Polynomial (ZMod 7) ⧸ span ({q} : Set $ Polynomial $ ZMod 7)) :=

p q : (ZMod 7)[X]
hp : p = X ^ 3 - 2
hq : q = X ^ 3 + 2
⊢ Irreducible p ∧ Irreducible q ∧ Nonempty ((ZMod 7)[X] ⧸ span {p} ≃+* (ZMod 7)[X] ⧸ span {q})


/-- If $p$ is a prime, show that $q(x) = 1 + x + x^2 + \cdots x^{p - 1}$ is irreducible in $Q[x]$.-/

theorem exercise_4_5_25 {p : ℕ} (hp : Nat.Prime p) :
  Irreducible (∑ i : Finset.range p, X ^ p : Polynomial ℚ) :=

p : ℕ
hp : p.Prime
⊢ Irreducible (∑ i : { x // x ∈ Finset.range p }, X ^ p)


/-- Prove that $f(x) = x^3 + 3x + 2$ is irreducible in $Q[x]$.-/

theorem exercise_4_6_2 : Irreducible (X^3 + 3*X + 2 : Polynomial ℚ) :=


/-- Show that there is an infinite number of integers a such that $f(x) = x^7 + 15x^2 - 30x + a$ is irreducible in $Q[x]$.-/

theorem exercise_4_6_3 :
  Infinite {a : ℤ | Irreducible (X^7 + 15*X^2 - 30*X + (a : Polynomial ℚ) : Polynomial ℚ)} :=

⊢ Infinite ↑{a | Irreducible (X ^ 7 + 15 * X ^ 2 - 30 * X + ↑a)}


/-- If $F$ is a field of characteristic $p \neq 0$, show that $(a + b)^m = a^m + b^m$, where $m = p^n$, for all $a, b \in F$ and any positive integer $n$.-/

theorem exercise_5_1_8 {p m n: ℕ} {F : Type*} [Field F]
  (hp : Nat.Prime p) (hF : CharP F p) (a b : F) (hm : m = p ^ n) :
  (a + b) ^ m = a^m + b^m :=

p m n : ℕ
F : Type u_1
inst✝ : Field F
hp : p.Prime
hF : CharP F p
a b : F
hm : m = p ^ n
⊢ (a + b) ^ m = a ^ m + b ^ m


/-- Let $V$ be a vector space over an infinite field $F$. Show that $V$ cannot be the set-theoretic union of a finite number of proper subspaces of $V$.-/

theorem exercise_5_2_20 {F V ι: Type*} [Infinite F] [Field F]
  [AddCommGroup V] [Module F V] {u : ι → Submodule F V}
  (hu : ∀ i : ι, u i ≠ ⊤) :
  (⋃ i : ι, (u i : Set V)) ≠ ⊤ :=

F : Type u_1
V : Type u_2
ι : Type u_3
inst✝³ : Infinite F
inst✝² : Field F
inst✝¹ : AddCommGroup V
inst✝ : Module F V
u : ι → Submodule F V
hu : ∀ (i : ι), u i ≠ ⊤
⊢ ⋃ i, ↑(u i) ≠ ⊤


/-- If $a \in K$ is such that $a^2$ is algebraic over the subfield $F$ of $K$, show that a is algebraic over $F$.-/

theorem exercise_5_3_7 {K : Type*} [Field K] {F : Subfield K}
  {a : K} (ha : IsAlgebraic F (a ^ 2)) : IsAlgebraic F a :=

K : Type u_1
inst✝ : Field K
F : Subfield K
a : K
ha : IsAlgebraic (↥F) (a ^ 2)
⊢ IsAlgebraic (↥F) a


/-- Prove that $\cos 1^{\circ}$  is algebraic over $\mathbb{Q}$.-/

theorem exercise_5_3_10 : IsAlgebraic ℚ (cos (Real.pi / 180)) :=


/-- If $a \in C$ is such that $p(a) = 0$, where $p(x) = x^5 + \sqrt{2}x^3 + \sqrt{5}x^2 + \sqrt{7}x + \sqrt{11}$, show that $a$ is algebraic over $\mathbb{Q}$ of degree at most 80.-/

theorem exercise_5_4_3 {a : ℂ} {p : ℂ → ℂ}
  (hp : p = λ (x : ℂ) => x^5 + sqrt 2 * x^3 + sqrt 5 * x^2 + sqrt 7 * x + 11)
  (ha : p a = 0) :
  ∃ p : Polynomial ℂ , p.degree < 80 ∧ a ∈ p.roots ∧
  ∀ n : p.support, ∃ a b : ℤ, p.coeff n = a / b :=

a : ℂ
p : ℂ → ℂ
hp : p = fun x => x ^ 5 + ↑√2 * x ^ 3 + ↑√5 * x ^ 2 + ↑√7 * x + 11
ha : p a = 0
⊢ ∃ p, p.degree < 80 ∧ a ∈ p.roots ∧ ∀ (n : { x // x ∈ p.support }), ∃ a b, p.coeff ↑n = ↑a / ↑b


/-- Prove that $x^3 - 3x - 1$ is irreducible over $\mathbb{Q}$.-/

theorem exercise_5_5_2 : Irreducible (X^3 - 3*X - 1 : Polynomial ℚ) :=


/-- If $F$ is of characteristic $p \neq 0$, show that all the roots of $x^m - x$, where $m = p^n$, are distinct.-/

theorem exercise_5_6_14 {p m n: ℕ} (hp : Nat.Prime p) {F : Type*}
  [Field F] [CharP F p] (hm : m = p ^ n) :
  card (rootSet (X ^ m - X : Polynomial F) F) = m :=

p m n : ℕ
hp : p.Prime
F : Type u_1
inst✝¹ : Field F
inst✝ : CharP F p
hm : m = p ^ n
⊢ card ↑((X ^ m - X).rootSet F) = m


/-- Let $(p_n)$ be a sequence and $f:\mathbb{N}\to\mathbb{N}$. The sequence $(q_k)_{k\in\mathbb{N}}$ with $q_k=p_{f(k)}$ is called a rearrangement of $(p_n)$. Show that if $f$ is an injection, the limit of a sequence is unaffected by rearrangement.-/

theorem exercise_2_12a (f : ℕ → ℕ) (p : ℕ → ℝ) (a : ℝ)
  (hf : Injective f) (hp : Tendsto p atTop (𝓝 a)) :
  Tendsto (λ n => p (f n)) atTop (𝓝 a) :=

f : ℕ → ℕ
p : ℕ → ℝ
a : ℝ
hf : Injective f
hp : Tendsto p atTop (𝓝 a)
⊢ Tendsto (fun n => p (f n)) atTop (𝓝 a)

import Mathlib

open Filter Real Function
open scoped Topology


/-- Prove that a set $U \subset M$ is open if and only if none of its points are limits of its complement.-/

theorem exercise_2_26 {M : Type*} [TopologicalSpace M]
  (U : Set M) : IsOpen U ↔ ∀ x ∈ U, ¬ ClusterPt x (𝓟 Uᶜ) :=

M : Type u_1
inst✝ : TopologicalSpace M
U : Set M
⊢ IsOpen U ↔ ∀ x ∈ U, ¬ClusterPt x (𝓟 Uᶜ)


/-- Let $\mathcal{T}$ be the collection of open subsets of a metric space $\mathrm{M}$, and $\mathcal{K}$ the collection of closed subsets. Show that there is a bijection from $\mathcal{T}$ onto $\mathcal{K}$.-/

theorem exercise_2_29 (M : Type*) [MetricSpace M]
  (O C : Set (Set M))
  (hO : O = {s | IsOpen s})
  (hC : C = {s | IsClosed s}) :
  ∃ f : O → C, Bijective f :=

M : Type u_1
inst✝ : MetricSpace M
O C : Set (Set M)
hO : O = {s | IsOpen s}
hC : C = {s | IsClosed s}
⊢ ∃ f, Bijective f


/-- Show that every subset of $\mathbb{N}$ is clopen.-/

theorem exercise_2_32a (A : Set ℕ) : IsClopen A :=


/-- Let $\|\cdot\|$ be any norm on $\mathbb{R}^{m}$ and let $B=\left\{x \in \mathbb{R}^{m}:\|x\| \leq 1\right\}$. Prove that $B$ is compact.-/

theorem exercise_2_41 (m : ℕ) {X : Type*} [NormedSpace ℝ ((Fin m) → ℝ)] :
  IsCompact (Metric.closedBall 0 1) :=

m : ℕ
X : Type u_1
inst✝ : NormedSpace ℝ (Fin m → ℝ)
⊢ IsCompact (Metric.closedBall 0 1)


/-- Assume that $A, B$ are compact, disjoint, nonempty subsets of $M$. Prove that there are $a_0 \in A$ and $b_0 \in B$ such that for all $a \in A$ and $b \in B$ we have $d(a_0, b_0) \leq d(a, b)$.-/

theorem exercise_2_46 {M : Type*} [MetricSpace M]
  {A B : Set M} (hA : IsCompact A) (hB : IsCompact B)
  (hAB : Disjoint A B) (hA₀ : A ≠ ∅) (hB₀ : B ≠ ∅) :
  ∃ a₀ b₀, a₀ ∈ A ∧ b₀ ∈ B ∧ ∀ (a : M) (b : M),
  a ∈ A → b ∈ B → dist a₀ b₀ ≤ dist a b :=

M : Type u_1
inst✝ : MetricSpace M
A B : Set M
hA : IsCompact A
hB : IsCompact B
hAB : Disjoint A B
hA₀ : A ≠ ∅
hB₀ : B ≠ ∅
⊢ ∃ a₀ b₀, a₀ ∈ A ∧ b₀ ∈ B ∧ ∀ (a b : M), a ∈ A → b ∈ B → dist a₀ b₀ ≤ dist a b


/-- Show that if $S$ is connected, it is not true in general that its interior is connected.-/

theorem exercise_2_57 {X : Type*} [TopologicalSpace X]
  : ∃ (S : Set X), IsConnected S ∧ ¬ IsConnected (interior S) :=

X : Type u_1
inst✝ : TopologicalSpace X
⊢ ∃ S, IsConnected S ∧ ¬IsConnected (interior S)


/-- Give a direct proof that the nested decreasing intersection of nonempty covering compact sets is nonempty.-/

theorem exercise_2_92 {α : Type*} [TopologicalSpace α]
  {s : ℕ → Set α}
  (hs : ∀ i, IsCompact (s i))
  (hs : ∀ i, (s i).Nonempty)
  (hs : ∀ i, (s i) ⊃ (s (i + 1))) :
  (⋂ i, s i).Nonempty :=

α : Type u_1
inst✝ : TopologicalSpace α
s : ℕ → Set α
hs✝¹ : ∀ (i : ℕ), IsCompact (s i)
hs✝ : ∀ (i : ℕ), (s i).Nonempty
hs : ∀ (i : ℕ), s i ⊃ s (i + 1)
⊢ (⋂ i, s i).Nonempty


/-- Suppose that $E$ is an uncountable subset of $\mathbb{R}$. Prove that there exists a point $p \in \mathbb{R}$ at which $E$ condenses.-/

theorem exercise_2_126 {E : Set ℝ}
  (hE : ¬ Set.Countable E) : ∃ (p : ℝ), ClusterPt p (𝓟 E) :=

E : Set ℝ
hE : ¬E.Countable
⊢ ∃ p, ClusterPt p (𝓟 E)


/-- Assume that $f \colon \mathbb{R} \rightarrow \mathbb{R}$ satisfies $|f(t)-f(x)| \leq|t-x|^{2}$ for all $t, x$. Prove that $f$ is constant.-/

theorem exercise_3_1 {f : ℝ → ℝ}
  (hf : ∀ x y, |f x - f y| ≤ |x - y| ^ 2) :
  ∃ c, f = λ x => c :=

f : ℝ → ℝ
hf : ∀ (x y : ℝ), |f x - f y| ≤ |x - y| ^ 2
⊢ ∃ c, f = fun x => c


/-- Prove that $\sqrt{n+1}-\sqrt{n} \rightarrow 0$ as $n \rightarrow \infty$.-/

theorem exercise_3_4 (n : ℕ) :
  Tendsto (λ n => (sqrt (n + 1) - sqrt n)) atTop (𝓝 0) :=

n : ℕ
⊢ Tendsto (fun n => √(n + 1) - √n) atTop (𝓝 0)


/-- Prove that $\sum 1/k(\log(k))^p$ converges when $p > 1$.-/

theorem exercise_3_63a (p : ℝ) (f : ℕ → ℝ) (hp : p > 1)
  (h : f = λ (k : ℕ) => (1 : ℝ) / (k * (log k) ^ p)) :
  ∃ l, Tendsto f atTop (𝓝 l) :=

p : ℝ
f : ℕ → ℝ
hp : p > 1
h : f = fun k => 1 / (↑k * (↑k).log ^ p)
⊢ ∃ l, Tendsto f atTop (𝓝 l)


/-- Prove that $\sum 1/k(\log(k))^p$ diverges when $p \leq 1$.-/

theorem exercise_3_63b (p : ℝ) (f : ℕ → ℝ) (hp : p ≤ 1)
  (h : f = λ (k : ℕ) => (1 : ℝ) / (k * (log k) ^ p)) :
  ¬ ∃ l, Tendsto f atTop (𝓝 l) :=

p : ℝ
f : ℕ → ℝ
hp : p ≤ 1
h : f = fun k => 1 / (↑k * (↑k).log ^ p)
⊢ ¬∃ l, Tendsto f atTop (𝓝 l)


/-- A continuous, strictly increasing function $\mu \colon (0, \infty) \rightarrow (0, \infty)$ is a modulus of continuity if $\mu(s) \rightarrow 0$ as $s \rightarrow 0$. A function $f \colon [a, b] \rightarrow \mathbb{R}$ has modulus of continuity $\mu$ if $|f(s) - f(t)| \leq \mu(|s - t|)$ for all $s, t \in [a, b]$. Prove that a function is uniformly continuous if and only if it has a modulus of continuity.-/

theorem exercise_4_15a {α : Type*}
  (a b : ℝ) (F : Set (ℝ → ℝ)) :
  (∀ x : ℝ, ∀ ε > 0, ∃ U ∈ (𝓝 x),
  (∀ y z : U, ∀ f : ℝ → ℝ, f ∈ F → (dist (f y) (f z) < ε)))
  ↔
  ∃ (μ : ℝ → ℝ), ∀ (x : ℝ), (0 : ℝ) ≤ μ x ∧ Tendsto μ (𝓝 0) (𝓝 0) ∧
  (∀ (s t : ℝ) (f : ℝ → ℝ), f ∈ F → |(f s) - (f t)| ≤ μ (|s - t|)) :=

α : Type u_1
a b : ℝ
F : Set (ℝ → ℝ)
⊢ (∀ (x ε : ℝ), ε > 0 → ∃ U ∈ 𝓝 x, ∀ (y z : ↑U), ∀ f ∈ F, dist (f ↑y) (f ↑z) < ε) ↔
    ∃ μ, ∀ (x : ℝ), 0 ≤ μ x ∧ Tendsto μ (𝓝 0) (𝓝 0) ∧ ∀ (s t : ℝ), ∀ f ∈ F, |f s - f t| ≤ μ |s - t|


/-- Let $H$ be the subgroup generated by two elements $a, b$ of a group $G$. Prove that if $a b=b a$, then $H$ is an abelian group.-/

theorem exercise_2_2_9 {G : Type} [Group G] {a b : G} (h : a * b = b * a) :
    ∀ x y : closure {x| x = a ∨ x = b}, x * y = y * x :=

G : Type
inst✝ : Group G
a b : G
h : a * b = b * a
⊢ ∀ (x y : ↥(Subgroup.closure {x | x = a ∨ x = b})), x * y = y * x

import Mathlib

open Function Fintype Subgroup Ideal Polynomial Submodule Zsqrtd
open scoped BigOperators


/-- Prove that the products $a b$ and $b a$ are conjugate elements in a group.-/

theorem exercise_2_3_2 {G : Type*} [Group G] (a b : G) :
    ∃ g : G, b* a = g * a * b * g⁻¹ :=

G : Type u_1
inst✝ : Group G
a b : G
⊢ ∃ g, b * a = g * a * b * g⁻¹


/-- Prove that if a group contains exactly one element of order 2 , then that element is in the center of the group.-/

theorem exercise_2_4_19 {G : Type*} [Group G] {x : G}
  (hx : orderOf x = 2) (hx1 : ∀ y, orderOf y = 2 → y = x) :
  x ∈ center G :=

G : Type u_1
inst✝ : Group G
x : G
hx : orderOf x = 2
hx1 : ∀ (y : G), orderOf y = 2 → y = x
⊢ x ∈ center G


/-- Prove that the center of the product of two groups is the product of their centers.-/

noncomputable def exercise_2_8_6 {G H : Type*} [Group G] [Group H] :
    center (G × H) ≃* (center G) × (center H) :=

G : Type u_1
H : Type u_2
inst✝¹ : Group G
inst✝ : Group H
⊢ ↥(center (G × H)) ≃* ↥(center G) × ↥(center H)

import Mathlib

open Function Fintype Subgroup Ideal Polynomial Submodule Zsqrtd
open scoped BigOperators

--center of (G × H) equivalent, preserves multiplication with (center G) × (center H)


/-- Prove that a group of even order contains an element of order $2 .$-/

theorem exercise_2_11_3 {G : Type*} [Group G] [Fintype G](hG : Even (card G)) :
  ∃ x : G, orderOf x = 2 :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : Even (card G)
⊢ ∃ x, orderOf x = 2


/-- Prove that every homomorphism of fields is injective.-/

theorem exercise_3_2_7 {F : Type*} [Field F] {G : Type*} [Field G]
    (φ : F →+* G) : Injective φ :=

F : Type u_1
inst✝¹ : Field F
G : Type u_2
inst✝ : Field G
φ : F →+* G
⊢ Injective ⇑φ

import Mathlib

open Function Fintype Subgroup Ideal Polynomial Submodule Zsqrtd
open scoped BigOperators

open RingHom


/-- Let $V$ be a vector space which is spanned by a countably infinite set. Prove that every linearly independent subset of $V$ is finite or countably infinite.-/

theorem exercise_3_5_6 {K V : Type*} [Field K] [AddCommGroup V]
  [Module K V] {S : Set V} (hS : Set.Countable S)
  (hS1 : span K S = ⊤) {ι : Type*} (R : ι → V)
  (hR : LinearIndependent K R) : Countable ι :=

K : Type u_1
V : Type u_2
inst✝² : Field K
inst✝¹ : AddCommGroup V
inst✝ : Module K V
S : Set V
hS : S.Countable
hS1 : Submodule.span K S = ⊤
ι : Type u_3
R : ι → V
hR : LinearIndependent K R
⊢ Countable ι


/-- Let $V$ be a vector space over an infinite field $F$. Prove that $V$ is not the union of finitely many proper subspaces.-/

theorem exercise_3_7_2 {K V : Type*} [Field K] [AddCommGroup V]
  [Module K V] {ι : Type*} [Fintype ι] (γ : ι → Submodule K V)
  (h : ∀ i : ι, γ i ≠ ⊤) :
  (⋂ (i : ι), (γ i : Set V)) ≠ ⊤ :=

K : Type u_1
V : Type u_2
inst✝³ : Field K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_3
inst✝ : Fintype ι
γ : ι → Submodule K V
h : ∀ (i : ι), γ i ≠ ⊤
⊢ ⋂ i, ↑(γ i) ≠ ⊤


/-- Let $Z$ be the center of a group $G$. Prove that if $G / Z$ is a cyclic group, then $G$ is abelian and hence $G=Z$.-/

theorem exercise_6_1_14 (G : Type*) [Group G]
  (hG : IsCyclic $ G ⧸ (center G)) :
  center G = ⊤ :=

G : Type u_1
inst✝ : Group G
hG : IsCyclic (G ⧸ center G)
⊢ center G = ⊤


/-- Prove that no group of order $p q$, where $p$ and $q$ are prime, is simple.-/

theorem exercise_6_4_2 {G : Type*} [Group G] [Fintype G] {p q : ℕ}
  (hp : Prime p) (hq : Prime q) (hG : card G = p*q) :
  IsSimpleGroup G → false :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
p q : ℕ
hp : Prime p
hq : Prime q
hG : card G = p * q
⊢ IsSimpleGroup G → false = true


/-- Prove that no group of order $p^2 q$, where $p$ and $q$ are prime, is simple.-/

theorem exercise_6_4_3 {G : Type*} [Group G] [Fintype G] {p q : ℕ}
  (hp : Prime p) (hq : Prime q) (hG : card G = p^2 *q) :
  IsSimpleGroup G → false :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
p q : ℕ
hp : Prime p
hq : Prime q
hG : card G = p ^ 2 * q
⊢ IsSimpleGroup G → false = true


/-- Prove that no group of order 224 is simple.-/

theorem exercise_6_4_12 {G : Type*} [Group G] [Fintype G]
  (hG : card G = 224) :
  IsSimpleGroup G → false :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 224
⊢ IsSimpleGroup G → false = true


/-- Prove that two elements $a, b$ of a group generate the same subgroup as $b a b^2, b a b^3$.-/

theorem exercise_6_8_1 {G : Type*} [Group G]
  (a b : G) : closure ({a, b} : Set G) = Subgroup.closure {b*a*b^2, b*a*b^3} :=

G : Type u_1
inst✝ : Group G
a b : G
⊢ Subgroup.closure {a, b} = Subgroup.closure {b * a * b ^ 2, b * a * b ^ 3}


/-- An element $x$ of a ring $R$ is called nilpotent if some power of $x$ is zero. Prove that if $x$ is nilpotent, then $1+x$ is a unit in $R$.-/

theorem exercise_10_1_13 {R : Type*} [Ring R] {x : R}
  (hx : IsNilpotent x) : IsUnit (1 + x) :=

R : Type u_1
inst✝ : Ring R
x : R
hx : IsNilpotent x
⊢ IsUnit (1 + x)


/-- Prove that in the ring $\mathbb{Z}[x],(2) \cap(x)=(2 x)$.-/

theorem exercise_10_2_4 :
  span ({2} : Set $ Polynomial ℤ) ⊓ (span {X}) =
  span ({2 * X} : Set $ Polynomial ℤ) :=

⊢ Ideal.span {2} ⊓ Ideal.span {X} = Ideal.span {2 * X}


/-- Prove that every nonzero ideal in the ring of Gauss integers contains a nonzero integer.-/

theorem exercise_10_6_7 {I : Ideal GaussianInt}
  (hI : I ≠ ⊥) : ∃ (z : I), z ≠ 0 ∧ (z : GaussianInt).im = 0 :=

I : Ideal GaussianInt
hI : I ≠ ⊥
⊢ ∃ z, z ≠ 0 ∧ (↑z).im = 0


/-- Let $I, J$ be ideals in a ring $R$. Prove that the residue of any element of $I \cap J$ in $R / I J$ is nilpotent.-/

theorem exercise_10_4_6 {R : Type*} [CommRing R]
  [NoZeroDivisors R] (I J : Ideal R) (x : ↑(I ⊓ J)) :
  IsNilpotent ((Ideal.Quotient.mk (I*J)) x) :=

R : Type u_1
inst✝¹ : CommRing R
inst✝ : NoZeroDivisors R
I J : Ideal R
x : ↥(I ⊓ J)
⊢ IsNilpotent ((Ideal.Quotient.mk (I * J)) ↑x)


/-- Let $I, J$ be ideals of a ring $R$ such that $I+J=R$. Prove that $I J=I \cap J$.-/

theorem exercise_10_4_7a {R : Type*} [CommRing R] [NoZeroDivisors R]
  (I J : Ideal R) (hIJ : I + J = ⊤) : I * J = I ⊓ J :=

R : Type u_1
inst✝¹ : CommRing R
inst✝ : NoZeroDivisors R
I J : Ideal R
hIJ : I + J = ⊤
⊢ I * J = I ⊓ J


/-- Let $R$ be a ring, with $M$ an ideal of $R$. Suppose that every element of $R$ which is not in $M$ is a unit of $R$. Prove that $M$ is a maximal ideal and that moreover it is the only maximal ideal of $R$.-/

theorem exercise_10_7_10 {R : Type*} [Ring R]
  (M : Ideal R) (hM : ∀ (x : R), x ∉ M → IsUnit x)
  (hProper : ∃ x : R, x ∉ M) :
  IsMaximal M ∧ ∀ (N : Ideal R), IsMaximal N → N = M :=

R : Type u_1
inst✝ : Ring R
M : Ideal R
hM : ∀ x ∉ M, IsUnit x
hProper : ∃ x, x ∉ M
⊢ M.IsMaximal ∧ ∀ (N : Ideal R), N.IsMaximal → N = M


/-- If $a, b$ are integers and if $a$ divides $b$ in the ring of Gauss integers, then $a$ divides $b$ in $\mathbb{Z}$.-/

theorem exercise_11_2_13 (a b : ℤ) :
  (ofInt a : GaussianInt) ∣ ofInt b → a ∣ b :=


/-- Prove that $x^3 + 6x + 12$ is irreducible in $\mathbb{Q}$.-/

theorem exercise_11_4_1b {F : Type*} [Field F] [Fintype F] (hF : card F = 2) :
  Irreducible (12 + 6 * X + X ^ 3 : Polynomial F) :=

F : Type u_1
inst✝¹ : Field F
inst✝ : Fintype F
hF : card F = 2
⊢ Irreducible (12 + 6 * X + X ^ 3)


/-- Prove that $x^2+x+1$ is irreducible in the field $\mathbb{F}_2$.-/

theorem exercise_11_4_6a {F : Type*} [Field F] [Fintype F] (hF : card F = 7) :
  Irreducible (X ^ 2 + 1 : Polynomial F) :=

F : Type u_1
inst✝¹ : Field F
inst✝ : Fintype F
hF : card F = 7
⊢ Irreducible (X ^ 2 + 1)


/-- Prove that $x^2+1$ is irreducible in $\mathbb{F}_7$-/

theorem exercise_11_4_6b {F : Type*} [Field F] [Fintype F] (hF : card F = 31) :
  Irreducible (X ^ 3 - 9 : Polynomial F) :=

F : Type u_1
inst✝¹ : Field F
inst✝ : Fintype F
hF : card F = 31
⊢ Irreducible (X ^ 3 - 9)


/-- Prove that $x^3 - 9$ is irreducible in $\mathbb{F}_{31}$.-/

theorem exercise_11_4_6c : Irreducible (X^3 - 9 : Polynomial (ZMod 31)) :=


/-- Let $p$ be a prime integer. Prove that the polynomial $x^n-p$ is irreducible in $\mathbb{Q}[x]$.-/

theorem exercise_11_4_8 (p : ℕ) (hp : Prime p) (n : ℕ) :
  -- p ∈ ℕ can be written as p ∈ ℚ[X]
  Irreducible (X ^ n - (p : Polynomial ℚ) : Polynomial ℚ) :=

p : ℕ
hp : Prime p
n : ℕ
⊢ Irreducible (X ^ n - ↑p)


/-- Prove that there are infinitely many primes congruent to $-1$ (modulo $4$).-/

theorem exercise_11_13_3 (N : ℕ):
  ∃ p ≥ N, Nat.Prime p ∧ p + 1 ≡ 0 [MOD 4] :=

N : ℕ
⊢ ∃ p ≥ N, p.Prime ∧ p + 1 ≡ 0 [MOD 4]


/-- Prove that if a prime integer $p$ has the form $2^r+1$, then it actually has the form $2^{2^k}+1$.-/

p : ℕ
hp : p.Prime
h : ∃ r, p = 2 ^ r + 1
⊢ ∃ k, p = 2 ^ 2 ^ k + 1


/-- Let $K$ be a finite field. Prove that the product of the nonzero elements of $K$ is $-1$.-/

theorem exercise_13_6_10 {K : Type*} [Field K] [Fintype Kˣ] :
  (∏ x : Kˣ,  x) = -1 :=

K : Type u_1
inst✝¹ : Field K
inst✝ : Fintype Kˣ
⊢ ∏ x : Kˣ, x = -1


/-- Show that $\frac{-1 + \sqrt{3}i}{2}$ is a cube root of 1 (meaning that its cube equals 1).-/

theorem exercise_1_2 :
  (⟨-1/2, Real.sqrt 3 / 2⟩ : ℂ) ^ 3 = -1 :=

import Mathlib

open Fintype Complex Polynomial LinearMap FiniteDimensional Module Module.End
open scoped BigOperators

open scoped BigOperators


/-- Prove that $-(-v) = v$ for every $v \in V$.-/

theorem exercise_1_3 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] {v : V} : -(-v) = v :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
v : V
⊢ - -v = v

import Mathlib

open Fintype Complex Polynomial LinearMap FiniteDimensional Module Module.End
open scoped BigOperators


/-- Prove that if $a \in \mathbf{F}$, $v \in V$, and $av = 0$, then $a = 0$ or $v = 0$.-/

theorem exercise_1_4 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] (v : V) (a : F): a • v = 0 ↔ a = 0 ∨ v = 0 :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
v : V
a : F
⊢ a • v = 0 ↔ a = 0 ∨ v = 0


/-- Give an example of a nonempty subset $U$ of $\mathbf{R}^2$ such that $U$ is closed under addition and under taking additive inverses (meaning $-u \in U$ whenever $u \in U$), but $U$ is not a subspace of $\mathbf{R}^2$.-/

theorem exercise_1_6 : ∃ U : Set (ℝ × ℝ),
  (U ≠ ∅) ∧
  (∀ (u v : ℝ × ℝ), u ∈ U ∧ v ∈ U → u + v ∈ U) ∧
  (∀ (u : ℝ × ℝ), u ∈ U → -u ∈ U) ∧
  (∀ U' : Submodule ℝ (ℝ × ℝ), U ≠ ↑U') :=

⊢ ∃ U, U ≠ ∅ ∧ (∀ (u v : ℝ × ℝ), u ∈ U ∧ v ∈ U → u + v ∈ U) ∧ (∀ u ∈ U, -u ∈ U) ∧ ∀ (U' : Submodule ℝ (ℝ × ℝ)), U ≠ ↑U'


/-- Give an example of a nonempty subset $U$ of $\mathbf{R}^2$ such that $U$ is closed under scalar multiplication, but $U$ is not a subspace of $\mathbf{R}^2$.-/

theorem exercise_1_7 : ∃ U : Set (ℝ × ℝ),
  (U ≠ ∅) ∧
  (∀ (c : ℝ) (u : ℝ × ℝ), u ∈ U → c • u ∈ U) ∧
  (∀ U' : Submodule ℝ (ℝ × ℝ), U ≠ ↑U') :=

⊢ ∃ U, U ≠ ∅ ∧ (∀ (c : ℝ), ∀ u ∈ U, c • u ∈ U) ∧ ∀ (U' : Submodule ℝ (ℝ × ℝ)), U ≠ ↑U'


/-- Prove that the intersection of any collection of subspaces of $V$ is a subspace of $V$.-/

theorem exercise_1_8 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] {ι : Type*} (u : ι → Submodule F V) :
  ∃ U : Submodule F V, (⋂ (i : ι), (u i).carrier) = ↑U :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
ι : Type u_3
u : ι → Submodule F V
⊢ ∃ U, ⋂ i, (u i).carrier = ↑U


/-- Prove that the union of two subspaces of $V$ is a subspace of $V$ if and only if one of the subspaces is contained in the other.-/

theorem exercise_1_9 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] (U W : Submodule F V):
  ∃ U' : Submodule F V, (U'.carrier = ↑U ∩ ↑W ↔ (U ≤ W ∨ W ≤ U)) :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
U W : Submodule F V
⊢ ∃ U', U'.carrier = ↑U ∩ ↑W ↔ U ≤ W ∨ W ≤ U


/-- Show that every linear map from a one-dimensional vector space to itself is multiplication by some scalar. More precisely, prove that if $\operatorname{dim} V=1$ and $T \in \mathcal{L}(V, V)$, then there exists $a \in \mathbf{F}$ such that $T v=a v$ for all $v \in V$.-/

theorem exercise_3_1 {F V : Type*}
  [AddCommGroup V] [Field F] [Module F V] [FiniteDimensional F V]
  (T : V →ₗ[F] V) (hT : finrank F V = 1) :
  ∃ c : F, ∀ v : V, T v = c • v :=

F : Type u_1
V : Type u_2
inst✝³ : AddCommGroup V
inst✝² : Field F
inst✝¹ : Module F V
inst✝ : FiniteDimensional F V
T : V →ₗ[F] V
hT : finrank F V = 1
⊢ ∃ c, ∀ (v : V), T v = c • v


/-- Suppose that $V$ is finite dimensional and that $T \in \mathcal{L}(V, W)$. Prove that there exists a subspace $U$ of $V$ such that $U \cap \operatorname{null} T=\{0\}$ and range $T=\{T u: u \in U\}$.-/

theorem exercise_3_8 {F V W : Type*}  [AddCommGroup V]
  [AddCommGroup W] [Field F] [Module F V] [Module F W]
  (L : V →ₗ[F] W) :
  ∃ U : Submodule F V, U ⊓ (ker L) = ⊥ ∧
  (range L = range (domRestrict L U)) :=

F : Type u_1
V : Type u_2
W : Type u_3
inst✝⁴ : AddCommGroup V
inst✝³ : AddCommGroup W
inst✝² : Field F
inst✝¹ : Module F V
inst✝ : Module F W
L : V →ₗ[F] W
⊢ ∃ U, U ⊓ ker L = ⊥ ∧ range L = range (L.domRestrict U)


/-- Suppose $p \in \mathcal{P}(\mathbf{C})$ has degree $m$. Prove that $p$ has $m$ distinct roots if and only if $p$ and its derivative $p^{\prime}$ have no roots in common.-/

theorem exercise_4_4 (p : Polynomial ℂ) :
  p.degree = @card (rootSet p ℂ) (rootSetFintype p ℂ) ↔
  Disjoint
  (@card (rootSet (derivative p) ℂ) (rootSetFintype (derivative p) ℂ))
  (@card (rootSet p ℂ) (rootSetFintype p ℂ)) :=

p : ℂ[X]
⊢ p.degree = ↑(card ↑(p.rootSet ℂ)) ↔ Disjoint (card ↑((derivative p).rootSet ℂ)) (card ↑(p.rootSet ℂ))


/-- Suppose $T \in \mathcal{L}(V)$. Prove that if $U_{1}, \ldots, U_{m}$ are subspaces of $V$ invariant under $T$, then $U_{1}+\cdots+U_{m}$ is invariant under $T$.-/

theorem exercise_5_1 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] {L : V →ₗ[F] V} {n : ℕ} (U : Fin n → Submodule F V)
  (hU : ∀ i : Fin n, Submodule.map L (U i) = U i) :
  Submodule.map L (∑ i : Fin n, U i : Submodule F V) =
  (∑ i : Fin n, U i : Submodule F V) :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
L : V →ₗ[F] V
n : ℕ
U : Fin n → Submodule F V
hU : ∀ (i : Fin n), Submodule.map L (U i) = U i
⊢ Submodule.map L (∑ i : Fin n, U i) = ∑ i : Fin n, U i


/-- Suppose that $S, T \in \mathcal{L}(V)$ are such that $S T=T S$. Prove that $\operatorname{null} (T-\lambda I)$ is invariant under $S$ for every $\lambda \in \mathbf{F}$.-/

theorem exercise_5_4 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] (S T : V →ₗ[F] V) (hST : S ∘ T = T ∘ S) (c : F):
  Submodule.map S (ker (T - c • LinearMap.id)) = ker (T - c • LinearMap.id) :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
S T : V →ₗ[F] V
hST : ⇑S ∘ ⇑T = ⇑T ∘ ⇑S
c : F
⊢ Submodule.map S (ker (T - c • LinearMap.id)) = ker (T - c • LinearMap.id)


/-- Suppose $S, T \in \mathcal{L}(V)$. Prove that $S T$ and $T S$ have the same eigenvalues.-/

theorem exercise_5_11 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] (S T : End F V) :
  (S * T).Eigenvalues = (T * S).Eigenvalues :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
S T : End F V
⊢ (S * T).Eigenvalues = (T * S).Eigenvalues


/-- Suppose $T \in \mathcal{L}(V)$ is such that every vector in $V$ is an eigenvector of $T$. Prove that $T$ is a scalar multiple of the identity operator.-/

theorem exercise_5_12 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] {S : End F V}
  (hS : ∀ v : V, ∃ c : F, v ∈ eigenspace S c) :
  ∃ c : F, S = c • LinearMap.id :=

F : Type u_1
V : Type u_2
inst✝² : AddCommGroup V
inst✝¹ : Field F
inst✝ : Module F V
S : End F V
hS : ∀ (v : V), ∃ c, v ∈ S.eigenspace c
⊢ ∃ c, S = c • LinearMap.id


/-- Suppose $T \in \mathcal{L}(V)$ is such that every subspace of $V$ with dimension $\operatorname{dim} V-1$ is invariant under $T$. Prove that $T$ is a scalar multiple of the identity operator.-/

theorem exercise_5_13 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] [FiniteDimensional F V] {T : End F V}
  (hS : ∀ U : Submodule F V, finrank F U = finrank F V - 1 →
  Submodule.map T U = U) : ∃ c : F, T = c • LinearMap.id :=

F : Type u_1
V : Type u_2
inst✝³ : AddCommGroup V
inst✝² : Field F
inst✝¹ : Module F V
inst✝ : FiniteDimensional F V
T : End F V
hS : ∀ (U : Submodule F V), finrank F ↥U = finrank F V - 1 → Submodule.map T U = U
⊢ ∃ c, T = c • LinearMap.id


/-- Suppose that $T \in \mathcal{L}(V)$ has $\operatorname{dim} V$ distinct eigenvalues and that $S \in \mathcal{L}(V)$ has the same eigenvectors as $T$ (not necessarily with the same eigenvalues). Prove that $S T=T S$.-/

theorem exercise_5_20 {F V : Type*} [AddCommGroup V] [Field F]
  [Module F V] [FiniteDimensional F V] {S T : End F V}
  (h1 : card (T.Eigenvalues) = finrank F V)
  (h2 : ∀ v : V, ∃ c : F, v ∈ eigenspace S c ↔ ∃ c : F, v ∈ eigenspace T c) :
  S * T = T * S :=

F : Type u_1
V : Type u_2
inst✝³ : AddCommGroup V
inst✝² : Field F
inst✝¹ : Module F V
inst✝ : FiniteDimensional F V
S T : End F V
h1 : card T.Eigenvalues = finrank F V
h2 : ∀ (v : V), ∃ c, v ∈ S.eigenspace c ↔ ∃ c, v ∈ T.eigenspace c
⊢ S * T = T * S


/-- Suppose $V$ is a real vector space and $T \in \mathcal{L}(V)$ has no eigenvalues. Prove that every subspace of $V$ invariant under $T$ has even dimension.-/

theorem exercise_5_24 {V : Type*} [AddCommGroup V]
  [Module ℝ V] [FiniteDimensional ℝ V] {T : End ℝ V}
  (hT : ∀ c : ℝ, eigenspace T c = ⊥) {U : Submodule ℝ V}
  (hU : Submodule.map T U = U) : Even (finrank U) :=

V : Type u_1
inst✝² : AddCommGroup V
inst✝¹ : Module ℝ V
inst✝ : FiniteDimensional ℝ V
T : End ℝ V
hT : ∀ (c : ℝ), T.eigenspace c = ⊥
U : Submodule ℝ V
hU : Submodule.map T U = U
⊢ Even (finrank ↥U)


/-- Suppose $u, v \in V$. Prove that $\langle u, v\rangle=0$ if and only if $\|u\| \leq\|u+a v\|$ for all $a \in \mathbf{F}$.-/

theorem exercise_6_2 {V : Type*} [NormedAddCommGroup V] [Module ℂ V]
[InnerProductSpace ℂ V] (u v : V) :
  ⟪u, v⟫_ℂ = 0 ↔ ∀ (a : ℂ), ‖u‖  ≤ ‖u + a • v‖ :=

V : Type u_1
inst✝² : NormedAddCommGroup V
inst✝¹ : Module ℂ V
inst✝ : InnerProductSpace ℂ V
u v : V
⊢ ⟪u, v⟫_ℂ = 0 ↔ ∀ (a : ℂ), ‖u‖ ≤ ‖u + a • v‖


theorem exercise_6_3 {n : ℕ} (a b : Fin n → ℝ) :
  (∑ i, a i * b i) ^ 2 ≤ (∑ i : Fin n, i * a i ^ 2) * (∑ i, b i ^ 2 / i) :=

n : ℕ
a b : Fin n → ℝ
⊢ (∑ i : Fin n, a i * b i) ^ 2 ≤ (∑ i : Fin n, ↑↑i * a i ^ 2) * ∑ i : Fin n, b i ^ 2 / ↑↑i


/-- Prove that if $V$ is a complex inner-product space, then $\langle u, v\rangle=\frac{\|u+v\|^{2}-\|u-v\|^{2}+\|u+i v\|^{2} i-\|u-i v\|^{2} i}{4}$ for all $u, v \in V$.-/

theorem exercise_6_7 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V] (u v : V) :
  ⟪u, v⟫_ℂ = (‖u + v‖^2 - ‖u - v‖^2 + I*‖u + I•v‖^2 - I*‖u-I•v‖^2) / 4 :=

V : Type u_1
inst✝¹ : NormedAddCommGroup V
inst✝ : InnerProductSpace ℂ V
u v : V
⊢ ⟪u, v⟫_ℂ = (↑‖u + v‖ ^ 2 - ↑‖u - v‖ ^ 2 + I * ↑‖u + I • v‖ ^ 2 - I * ↑‖u - I • v‖ ^ 2) / 4


/-- Suppose $\left(e_{1}, \ldots, e_{m}\right)$ is an or thonormal list of vectors in $V$. Let $v \in V$. Prove that $\|v\|^{2}=\left|\left\langle v, e_{1}\right\rangle\right|^{2}+\cdots+\left|\left\langle v, e_{m}\right\rangle\right|^{2}$ if and only if $v \in \operatorname{span}\left(e_{1}, \ldots, e_{m}\right)$.-/

theorem exercise_6_13 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V] {n : ℕ}
  {e : Fin n → V} (he : Orthonormal ℂ e) (v : V) :
  ‖v‖^2 = ∑ i : Fin n, ‖⟪v, e i⟫_ℂ‖^2 ↔ v ∈ Submodule.span ℂ (e '' Set.univ) :=

V : Type u_1
inst✝¹ : NormedAddCommGroup V
inst✝ : InnerProductSpace ℂ V
n : ℕ
e : Fin n → V
he : Orthonormal ℂ e
v : V
⊢ ‖v‖ ^ 2 = ∑ i : Fin n, ‖⟪v, e i⟫_ℂ‖ ^ 2 ↔ v ∈ Submodule.span ℂ (e '' Set.univ)


/-- Suppose $U$ is a subspace of $V$. Prove that $U^{\perp}=\{0\}$ if and only if $U=V$-/

theorem exercise_6_16 {K V : Type*} [RCLike K] [NormedAddCommGroup V] [InnerProductSpace K V]
  {U : Submodule K V} :
  U.orthogonal = ⊥  ↔ U = ⊤ :=

K : Type u_1
V : Type u_2
inst✝² : RCLike K
inst✝¹ : NormedAddCommGroup V
inst✝ : InnerProductSpace K V
U : Submodule K V
⊢ Uᗮ = ⊥ ↔ U = ⊤


/-- Show that if $\operatorname{dim} V \geq 2$, then the set of normal operators on $V$ is not a subspace of $\mathcal{L}(V)$.-/

theorem exercise_7_5 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V]
  [FiniteDimensional ℂ V] (hV : finrank V ≥ 2) :
  ∀ U : Submodule ℂ (End ℂ V), U.carrier ≠
  {T | T * adjoint T = adjoint T * T} :=

V : Type u_1
inst✝² : NormedAddCommGroup V
inst✝¹ : InnerProductSpace ℂ V
inst✝ : FiniteDimensional ℂ V
hV : finrank V ≥ 2
⊢ ∀ (U : Submodule ℂ (End ℂ V)), U.carrier ≠ {T | T * adjoint T = adjoint T * T}


/-- Prove that if $T \in \mathcal{L}(V)$ is normal, then $\operatorname{range} T=\operatorname{range} T^{*}.$-/

theorem exercise_7_6 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V]
  [FiniteDimensional ℂ V] (T : End ℂ V)
  (hT : T * adjoint T = adjoint T * T) :
  range T = range (adjoint T) :=

V : Type u_1
inst✝² : NormedAddCommGroup V
inst✝¹ : InnerProductSpace ℂ V
inst✝ : FiniteDimensional ℂ V
T : End ℂ V
hT : T * adjoint T = adjoint T * T
⊢ range T = range (adjoint T)


/-- Prove that a normal operator on a complex inner-product space is self-adjoint if and only if all its eigenvalues are real.-/

theorem exercise_7_9 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V]
  [FiniteDimensional ℂ V] (T : End ℂ V)
  (hT : T * adjoint T = adjoint T * T) :
  IsSelfAdjoint T ↔ ∀ e : T.Eigenvalues, (e : ℂ).im = 0 :=

V : Type u_1
inst✝² : NormedAddCommGroup V
inst✝¹ : InnerProductSpace ℂ V
inst✝ : FiniteDimensional ℂ V
T : End ℂ V
hT : T * adjoint T = adjoint T * T
⊢ IsSelfAdjoint T ↔ ∀ (e : T.Eigenvalues), (↑T e).im = 0


/-- Suppose $V$ is a complex inner-product space and $T \in \mathcal{L}(V)$ is a normal operator such that $T^{9}=T^{8}$. Prove that $T$ is self-adjoint and $T^{2}=T$.-/

theorem exercise_7_10 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V]
  [FiniteDimensional ℂ V] (T : End ℂ V)
  (hT : T * adjoint T = adjoint T * T) (hT1 : T^9 = T^8) :
  IsSelfAdjoint T ∧ T^2 = T :=

V : Type u_1
inst✝² : NormedAddCommGroup V
inst✝¹ : InnerProductSpace ℂ V
inst✝ : FiniteDimensional ℂ V
T : End ℂ V
hT : T * adjoint T = adjoint T * T
hT1 : T ^ 9 = T ^ 8
⊢ IsSelfAdjoint T ∧ T ^ 2 = T


/-- Suppose $V$ is a complex inner-product space. Prove that every normal operator on $V$ has a square root. (An operator $S \in \mathcal{L}(V)$ is called a square root of $T \in \mathcal{L}(V)$ if $S^{2}=T$.)-/

theorem exercise_7_11 {V : Type*} [NormedAddCommGroup V] [InnerProductSpace ℂ V]
  [FiniteDimensional ℂ V] {T : End ℂ V} (hT : T*adjoint T = adjoint T*T) :
  ∃ (S : End ℂ V), S ^ 2 = T :=

V : Type u_1
inst✝² : NormedAddCommGroup V
inst✝¹ : InnerProductSpace ℂ V
inst✝ : FiniteDimensional ℂ V
T : End ℂ V
hT : T * adjoint T = adjoint T * T
⊢ ∃ S, S ^ 2 = T


/-- Suppose $T \in \mathcal{L}(V)$ is self-adjoint, $\lambda \in \mathbf{F}$, and $\epsilon>0$. Prove that if there exists $v \in V$ such that $\|v\|=1$ and $\|T v-\lambda v\|<\epsilon,$ then $T$ has an eigenvalue $\lambda^{\prime}$ such that $\left|\lambda-\lambda^{\prime}\right|<\epsilon$.-/

theorem exercise_7_14 {𝕜 V : Type*} [RCLike 𝕜] [NormedAddCommGroup V]
  [InnerProductSpace 𝕜 V] [FiniteDimensional 𝕜 V]
  {T : Module.End 𝕜 V} (hT : IsSelfAdjoint T)
  {l : 𝕜} {ε : ℝ} (he : ε > 0) : ∃ v : V, ‖v‖= 1 ∧ (‖T v - l • v‖ < ε →
  (∃ l' : T.Eigenvalues, ‖l - l'‖ < ε)) :=

𝕜 : Type u_1
V : Type u_2
inst✝³ : RCLike 𝕜
inst✝² : NormedAddCommGroup V
inst✝¹ : InnerProductSpace 𝕜 V
inst✝ : FiniteDimensional 𝕜 V
T : End 𝕜 V
hT : IsSelfAdjoint T
l : 𝕜
ε : ℝ
he : ε > 0
⊢ ∃ v, ‖v‖ = 1 ∧ (‖T v - l • v‖ < ε → ∃ l', ‖l - ↑T l'‖ < ε)


/-- Prove the the operation $\star$ on $\mathbb{Z}$ defined by $a\star b=a-b$ is not commutative.-/

theorem exercise_1_1_2a : ∃ a b : ℤ, a - b ≠ b - a :=

import Mathlib

open Fintype Subgroup Set Polynomial Ideal
open scoped BigOperators


/-- Prove that the addition of residue classes $\mathbb{Z}/n\mathbb{Z}$ is associative.-/

theorem exercise_1_1_3 (n : ℤ) :
  ∀ (a b c : ℤ), (a+b)+c ≡ a+(b+c) [ZMOD n] :=

n : ℤ
⊢ ∀ (a b c : ℤ), a + b + c ≡ a + (b + c) [ZMOD n]


/-- Prove that the multiplication of residue class $\mathbb{Z}/n\mathbb{Z}$ is associative.-/

theorem exercise_1_1_4 (n : ℕ) :
  ∀ (a b c : ℕ), (a * b) * c ≡ a * (b * c) [ZMOD n] :=

n : ℕ
⊢ ∀ (a b c : ℕ), ↑a * ↑b * ↑c ≡ ↑a * (↑b * ↑c) [ZMOD ↑n]


/-- Prove that for all $n>1$ that $\mathbb{Z}/n\mathbb{Z}$ is not a group under multiplication of residue classes.-/

theorem exercise_1_1_5 (n : ℕ) (hn : 1 < n) :
  IsEmpty (Group (ZMod n)) :=

n : ℕ
hn : 1 < n
⊢ IsEmpty (Group (ZMod n))


theorem exercise_1_1_15 {G : Type*} [Group G] (as : List G) :
  as.prod⁻¹ = (as.reverse.map (λ x => x⁻¹)).prod :=

G : Type u_1
inst✝ : Group G
as : List G
⊢ as.prod⁻¹ = (List.map (fun x => x⁻¹) as.reverse).prod


/-- Let $x$ be an element of $G$. Prove that $x^2=1$ if and only if $|x|$ is either $1$ or $2$.-/

theorem exercise_1_1_16 {G : Type*} [Group G]
  (x : G) (hx : x ^ 2 = 1) :
  orderOf x = 1 ∨ orderOf x = 2 :=

G : Type u_1
inst✝ : Group G
x : G
hx : x ^ 2 = 1
⊢ orderOf x = 1 ∨ orderOf x = 2


/-- Let $x$ be an element of $G$. Prove that if $|x|=n$ for some positive integer $n$ then $x^{-1}=x^{n-1}$.-/

theorem exercise_1_1_17 {G : Type*} [Group G] {x : G} {n : ℕ}
  (hxn: orderOf x = n) :
  x⁻¹ = x ^ (n - 1 : ℤ) :=

G : Type u_1
inst✝ : Group G
x : G
n : ℕ
hxn : orderOf x = n
⊢ x⁻¹ = x ^ (↑n - 1)


/-- Let $x$ and $y$ be elements of $G$. Prove that $xy=yx$ if and only if $y^{-1}xy=x$ if and only if $x^{-1}y^{-1}xy=1$.-/

theorem exercise_1_1_18 {G : Type*} [Group G]
  (x y : G) : (x * y = y * x ↔ y⁻¹ * x * y = x) ↔ (x⁻¹ * y⁻¹ * x * y = 1) :=

G : Type u_1
inst✝ : Group G
x y : G
⊢ (x * y = y * x ↔ y⁻¹ * x * y = x) ↔ x⁻¹ * y⁻¹ * x * y = 1


/-- For $x$ an element in $G$ show that $x$ and $x^{-1}$ have the same order.-/

theorem exercise_1_1_20 {G : Type*} [Group G] {x : G} :
  orderOf x = orderOf x⁻¹ :=

G : Type u_1
inst✝ : Group G
x : G
⊢ orderOf x = orderOf x⁻¹


/-- If $x$ and $g$ are elements of the group $G$, prove that $|x|=\left|g^{-1} x g\right|$.-/

theorem exercise_1_1_22a {G : Type*} [Group G] (x g : G) :
  orderOf x = orderOf (g⁻¹ * x * g) :=

G : Type u_1
inst✝ : Group G
x g : G
⊢ orderOf x = orderOf (g⁻¹ * x * g)


/-- Deduce that $|a b|=|b a|$ for all $a, b \in G$.-/

theorem exercise_1_1_22b {G: Type*} [Group G] (a b : G) :
  orderOf (a * b) = orderOf (b * a) :=

G : Type u_1
inst✝ : Group G
a b : G
⊢ orderOf (a * b) = orderOf (b * a)


/-- Prove that if $x^{2}=1$ for all $x \in G$ then $G$ is abelian.-/

theorem exercise_1_1_25 {G : Type*} [Group G]
  (h : ∀ x : G, x ^ 2 = 1) : ∀ a b : G, a*b = b*a :=

G : Type u_1
inst✝ : Group G
h : ∀ (x : G), x ^ 2 = 1
⊢ ∀ (a b : G), a * b = b * a


/-- Prove that $A \times B$ is an abelian group if and only if both $A$ and $B$ are abelian.-/

theorem exercise_1_1_29 {A B : Type*} [Group A] [Group B] :
  ∀ x y : A × B, x*y = y*x ↔ (∀ x y : A, x*y = y*x) ∧
  (∀ x y : B, x*y = y*x) :=

A : Type u_1
B : Type u_2
inst✝¹ : Group A
inst✝ : Group B
⊢ ∀ (x y : A × B), x * y = y * x ↔ (∀ (x y : A), x * y = y * x) ∧ ∀ (x y : B), x * y = y * x


/-- If $x$ is an element of infinite order in $G$, prove that the elements $x^{n}, n \in \mathbb{Z}$ are all distinct.-/

theorem exercise_1_1_34 {G : Type*} [Group G] {x : G}
  (hx_inf : orderOf x = 0) (n m : ℤ) :
  x ^ n ≠ x ^ m :=

G : Type u_1
inst✝ : Group G
x : G
hx_inf : orderOf x = 0
n m : ℤ
⊢ x ^ n ≠ x ^ m


/-- Prove that if $\Omega=\{1,2,3, \ldots\}$ then $S_{\Omega}$ is an infinite group-/

theorem exercise_1_3_8 : Infinite (Equiv.Perm ℕ) :=


/-- Prove that the multiplicative groups $\mathbb{R}-\{0\}$ and $\mathbb{C}-\{0\}$ are not isomorphic.-/

theorem exercise_1_6_4 :
  IsEmpty (Multiplicative ℝ ≃* Multiplicative ℂ) :=

⊢ IsEmpty (Multiplicative ℝ ≃* Multiplicative ℂ)


/-- Let $A$ and $B$ be groups. Prove that $A \times B \cong B \times A$.-/

noncomputable def exercise_1_6_11 {A B : Type*} [Group A] [Group B] :
  A × B ≃* B × A :=

A : Type u_1
B : Type u_2
inst✝¹ : Group A
inst✝ : Group B
⊢ A × B ≃* B × A


/-- Let $G$ be any group. Prove that the map from $G$ to itself defined by $g \mapsto g^{-1}$ is a homomorphism if and only if $G$ is abelian.-/

theorem exercise_1_6_17 {G : Type*} [Group G] (f : G → G)
  (hf : f = λ g => g⁻¹) :
  ∀ x y : G, f x * f y = f (x*y) ↔ ∀ x y : G, x*y = y*x :=

G : Type u_1
inst✝ : Group G
f : G → G
hf : f = fun g => g⁻¹
⊢ ∀ (x y : G), f x * f y = f (x * y) ↔ ∀ (x y : G), x * y = y * x


/-- Let $G$ be a finite group which possesses an automorphism $\sigma$ such that $\sigma(g)=g$ if and only if $g=1$. If $\sigma^{2}$ is the identity map from $G$ to $G$, prove that $G$ is abelian.-/

theorem exercise_1_6_23 {G : Type*}
  [Group G] (σ : MulAut G) (hs : ∀ g : G, σ g = 1 → g = 1)
  (hs2 : ∀ g : G, σ (σ g) = g) :
  ∀ x y : G, x*y = y*x :=

G : Type u_1
inst✝ : Group G
σ : MulAut G
hs : ∀ (g : G), σ g = 1 → g = 1
hs2 : ∀ (g : G), σ (σ g) = g
⊢ ∀ (x y : G), x * y = y * x


/-- Prove that $G$ cannot have a subgroup $H$ with $|H|=n-1$, where $n=|G|>2$.-/

theorem exercise_2_1_5 {G : Type*} [Group G] [Fintype G]
  (hG : card G > 2) (H : Subgroup G) [Fintype H] :
  card H ≠ card G - 1 :=

G : Type u_1
inst✝² : Group G
inst✝¹ : Fintype G
hG : card G > 2
H : Subgroup G
inst✝ : Fintype ↥H
⊢ card ↥H ≠ card G - 1


/-- Let $H$ be a subgroup of the additive group of rational numbers with the property that $1 / x \in H$ for every nonzero element $x$ of $H$. Prove that $H=0$ or $\mathbb{Q}$.-/

theorem exercise_2_1_13 (H : AddSubgroup ℚ) {x : ℚ}
  (hH : x ∈ H → (1 / x) ∈ H):
  H = ⊥ ∨ H = ⊤ :=

H : AddSubgroup ℚ
x : ℚ
hH : x ∈ H → 1 / x ∈ H
⊢ H = ⊥ ∨ H = ⊤


/-- Prove that if $H$ is a subgroup of $G$ then $H$ is generated by the set $H-\{1\}$.-/

theorem exercise_2_4_4 {G : Type*} [Group G] (H : Subgroup G) :
  closure ((H : Set G) \ {1}) = ⊤ :=

G : Type u_1
inst✝ : Group G
H : Subgroup G
⊢ Subgroup.closure (↑H \ {1}) = ⊤


/-- A subgroup $M$ of a group $G$ is called a maximal subgroup if $M \neq G$ and the only subgroups of $G$ which contain $M$ are $M$ and $G$. Prove that if $H$ is a proper subgroup of the finite group $G$ then there is a maximal subgroup of $G$ containing $H$.-/

theorem exercise_2_4_16a {G : Type*} [Group G] {H : Subgroup G}
  (hH : H ≠ ⊤) :
  ∃ M : Subgroup G, M ≠ ⊤ ∧
  ∀ K : Subgroup G, M ≤ K → K = M ∨ K = ⊤ ∧
  H ≤ M :=

G : Type u_1
inst✝ : Group G
H : Subgroup G
hH : H ≠ ⊤
⊢ ∃ M, M ≠ ⊤ ∧ ∀ (K : Subgroup G), M ≤ K → K = M ∨ K = ⊤ ∧ H ≤ M


/-- Show that the subgroup of all rotations in a dihedral group is a maximal subgroup.-/

theorem exercise_2_4_16b {n : ℕ} {hn : n ≠ 0}
  {R : Subgroup (DihedralGroup n)}
  (hR : R = Subgroup.closure {DihedralGroup.r 1}) :
  R ≠ ⊤ ∧
  ∀ K : Subgroup (DihedralGroup n), R ≤ K → K = R ∨ K = ⊤ :=

n : ℕ
hn : n ≠ 0
R : Subgroup (DihedralGroup n)
hR : R = Subgroup.closure {DihedralGroup.r 1}
⊢ R ≠ ⊤ ∧ ∀ (K : Subgroup (DihedralGroup n)), R ≤ K → K = R ∨ K = ⊤


/-- Show that if $G=\langle x\rangle$ is a cyclic group of order $n \geq 1$ then a subgroup $H$ is maximal if and only $H=\left\langle x^{p}\right\rangle$ for some prime $p$ dividing $n$.-/

theorem exercise_2_4_16c {n : ℕ} (H : AddSubgroup (ZMod n)) :
  ∃ p : (ZMod n), Prime p ∧ H = AddSubgroup.closure {p} ↔
  (H ≠ ⊤ ∧ ∀ K : AddSubgroup (ZMod n), H ≤ K → K = H ∨ K = ⊤) :=

n : ℕ
H : AddSubgroup (ZMod n)
⊢ ∃ p, Prime p ∧ H = AddSubgroup.closure {p} ↔ H ≠ ⊤ ∧ ∀ (K : AddSubgroup (ZMod n)), H ≤ K → K = H ∨ K = ⊤


/-- Let $A$ be an abelian group and let $B$ be a subgroup of $A$. Prove that $A / B$ is abelian.-/

theorem exercise_3_1_3a {A : Type*} [CommGroup A] (B : Subgroup A) :
  ∀ a b : A ⧸ B, a*b = b*a :=

A : Type u_1
inst✝ : CommGroup A
B : Subgroup A
⊢ ∀ (a b : A ⧸ B), a * b = b * a


/-- Prove that if $H$ and $K$ are normal subgroups of a group $G$ then their intersection $H \cap K$ is also a normal subgroup of $G$.-/

theorem exercise_3_1_22a (G : Type*) [Group G] (H K : Subgroup G)
  [Normal H] [Normal K] :
  Normal (H ⊓ K) :=

G : Type u_1
inst✝² : Group G
H K : Subgroup G
inst✝¹ : H.Normal
inst✝ : K.Normal
⊢ (H ⊓ K).Normal


/-- Prove that the intersection of an arbitrary nonempty collection of normal subgroups of a group is a normal subgroup (do not assume the collection is countable).-/

theorem exercise_3_1_22b {G : Type*} [Group G] (I : Type*)
  (H : I → Subgroup G) (hH : ∀ i : I, Normal (H i)) :
  Normal (⨅ (i : I), H i) :=

G : Type u_1
inst✝ : Group G
I : Type u_2
H : I → Subgroup G
hH : ∀ (i : I), (H i).Normal
⊢ (⨅ i, H i).Normal


/-- Prove that if $H$ and $K$ are finite subgroups of $G$ whose orders are relatively prime then $H \cap K=1$.-/

theorem exercise_3_2_8 {G : Type*} [Group G] (H K : Subgroup G)
  [Fintype H] [Fintype K]
  (hHK : Nat.Coprime (card H) (card K)) :
  H ⊓ K = ⊥ :=

G : Type u_1
inst✝² : Group G
H K : Subgroup G
inst✝¹ : Fintype ↥H
inst✝ : Fintype ↥K
hHK : (card ↥H).Coprime (card ↥K)
⊢ H ⊓ K = ⊥


/-- Let $H \leq K \leq G$. Prove that $|G: H|=|G: K| \cdot|K: H|$ (do not assume $G$ is finite).-/

theorem exercise_3_2_11 {G : Type*} [Group G] {H K : Subgroup G}
  (hHK : H ≤ K) :
  H.index = K.index * H.relindex K :=

G : Type u_1
inst✝ : Group G
H K : Subgroup G
hHK : H ≤ K
⊢ H.index = K.index * H.relindex K


/-- Use Lagrange's Theorem in the multiplicative group $(\mathbb{Z} / p \mathbb{Z})^{\times}$to prove Fermat's Little Theorem: if $p$ is a prime then $a^{p} \equiv a(\bmod p)$ for all $a \in \mathbb{Z}$.-/

theorem exercise_3_2_16 (p : ℕ) (hp : Nat.Prime p) (a : ℕ) :
  Nat.Coprime a p → a ^ p ≡ a [ZMOD p] :=

p : ℕ
hp : p.Prime
a : ℕ
⊢ a.Coprime p → ↑a ^ p ≡ ↑a [ZMOD ↑p]


/-- Prove that $\mathbb{Q}$ has no proper subgroups of finite index.-/

theorem exercise_3_2_21a (H : AddSubgroup ℚ) (hH : H ≠ ⊤) : H.index = 0 :=

H : AddSubgroup ℚ
hH : H ≠ ⊤
⊢ H.index = 0


/-- Prove that if $H$ is a normal subgroup of $G$ of prime index $p$ then for all $K \leq G$ either $K \leq H$, or $G=H K$ and $|K: K \cap H|=p$.-/

theorem exercise_3_3_3 {p : Nat.Primes} {G : Type*} [Group G]
  {H : Subgroup G} [hH : H.Normal] (hH1 : H.index = p) :
  ∀ K : Subgroup G, K ≤ H ∨ H ⊔ K = ⊤ ∨ (K ⊓ H).relindex K = p :=

p : Nat.Primes
G : Type u_1
inst✝ : Group G
H : Subgroup G
hH : H.Normal
hH1 : H.index = ↑p
⊢ ∀ (K : Subgroup G), K ≤ H ∨ H ⊔ K = ⊤ ∨ (K ⊓ H).relindex K = ↑p


/-- Prove that if $G$ is an abelian simple group then $G \cong Z_{p}$ for some prime $p$ (do not assume $G$ is a finite group).-/

theorem exercise_3_4_1 (G : Type*) [CommGroup G] [IsSimpleGroup G] :
    IsCyclic G ∧ ∃ G_fin : Fintype G, Nat.Prime (@card G G_fin) :=

G : Type u_1
inst✝¹ : CommGroup G
inst✝ : IsSimpleGroup G
⊢ IsCyclic G ∧ ∃ G_fin, (card G).Prime


/-- Use Cauchy's Theorem and induction to show that a finite abelian group has a subgroup of order $n$ for each positive divisor $n$ of its order.-/

theorem exercise_3_4_4 {G : Type*} [CommGroup G] [Fintype G] {n : ℕ}
    (hn : n ∣ (card G)) :
    ∃ (H : Subgroup G) (H_fin : Fintype H), @card H H_fin = n :=

G : Type u_1
inst✝¹ : CommGroup G
inst✝ : Fintype G
n : ℕ
hn : n ∣ card G
⊢ ∃ H H_fin, card ↥H = n


/-- Prove that subgroups of a solvable group are solvable.-/

theorem exercise_3_4_5a {G : Type*} [Group G]
  (H : Subgroup G) [IsSolvable G] : IsSolvable H :=

G : Type u_1
inst✝¹ : Group G
H : Subgroup G
inst✝ : IsSolvable G
⊢ IsSolvable ↥H


/-- Prove that quotient groups of a solvable group are solvable.-/

theorem exercise_3_4_5b {G : Type*} [Group G] [IsSolvable G]
  (H : Subgroup G) [Normal H] :
  IsSolvable (G ⧸ H) :=

G : Type u_1
inst✝² : Group G
inst✝¹ : IsSolvable G
H : Subgroup G
inst✝ : H.Normal
⊢ IsSolvable (G ⧸ H)


/-- Prove that if $H$ is a nontrivial normal subgroup of the solvable group $G$ then there is a nontrivial subgroup $A$ of $H$ with $A \unlhd G$ and $A$ abelian.-/

theorem exercise_3_4_11 {G : Type*} [Group G] [IsSolvable G]
  {H : Subgroup G} (hH : H ≠ ⊥) [H.Normal] :
  ∃ A ≤ H, A.Normal ∧ ∀ a b : A, a*b = b*a :=

G : Type u_1
inst✝² : Group G
inst✝¹ : IsSolvable G
H : Subgroup G
hH : H ≠ ⊥
inst✝ : H.Normal
⊢ ∃ A ≤ H, A.Normal ∧ ∀ (a b : ↥A), a * b = b * a


/-- Prove that if $H$ has finite index $n$ then there is a normal subgroup $K$ of $G$ with $K \leq H$ and $|G: K| \leq n!$.-/

theorem exercise_4_2_8 {G : Type*} [Group G] {H : Subgroup G}
  {n : ℕ} (hn : n > 0) (hH : H.index = n) :
  ∃ K ≤ H, K.Normal ∧ K.index ≤ n.factorial :=

G : Type u_1
inst✝ : Group G
H : Subgroup G
n : ℕ
hn : n > 0
hH : H.index = n
⊢ ∃ K ≤ H, K.Normal ∧ K.index ≤ n.factorial


/-- Let $G$ be a transitive permutation group on the finite set $A$ with $|A|>1$. Show that there is some $\sigma \in G$ such that $\sigma(a) \neq a$ for all $a \in A$.-/

theorem exercise_4_3_26 {α : Type*} [Fintype α] (ha : card α > 1)
  (h_tran : ∀ a b: α, ∃ σ : Equiv.Perm α, σ a = b) :
  ∃ σ : Equiv.Perm α, ∀ a : α, σ a ≠ a :=

α : Type u_1
inst✝ : Fintype α
ha : card α > 1
h_tran : ∀ (a b : α), ∃ σ, σ a = b
⊢ ∃ σ, ∀ (a : α), σ a ≠ a


/-- Prove that if $p$ is a prime and $G$ is a group of order $p^{\alpha}$ for some $\alpha \in \mathbb{Z}^{+}$, then every subgroup of index $p$ is normal in $G$.-/

theorem exercise_4_2_9a {G : Type*} [Fintype G] [Group G] {p α : ℕ}
  (hp : p.Prime) (ha : α > 0) (hG : card G = p ^ α) :
  ∀ H : Subgroup G, H.index = p → H.Normal :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
p α : ℕ
hp : p.Prime
ha : α > 0
hG : card G = p ^ α
⊢ ∀ (H : Subgroup G), H.index = p → H.Normal


/-- Let $G$ be a finite group of composite order $n$ with the property that $G$ has a subgroup of order $k$ for each positive integer $k$ dividing $n$. Prove that $G$ is not simple.-/

theorem exercise_4_2_14 {G : Type*} [Fintype G] [Group G]
  (hG : ¬ (card G).Prime) (hG1 : ∀ k : ℕ, k ∣ card G →
  ∃ (H : Subgroup G) (fH : Fintype H), @card H fH = k) :
  ¬ IsSimpleGroup G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : ¬(card G).Prime
hG1 : ∀ (k : ℕ), k ∣ card G → ∃ H fH, card ↥H = k
⊢ ¬IsSimpleGroup G


/-- Prove that if $G$ is an abelian group of order $p q$, where $p$ and $q$ are distinct primes, then $G$ is cyclic.-/

theorem exercise_4_4_2 {G : Type*} [Fintype G] [Group G]
  {p q : Nat.Primes} (hpq : p ≠ q) (hG : card G = p*q) :
  IsCyclic G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
p q : Nat.Primes
hpq : p ≠ q
hG : card G = ↑p * ↑q
⊢ IsCyclic G


/-- Prove that characteristic subgroups are normal.-/

theorem exercise_4_4_6a {G : Type*} [Group G] (H : Subgroup G)
  [Characteristic H] : Normal H :=

G : Type u_1
inst✝¹ : Group G
H : Subgroup G
inst✝ : H.Characteristic
⊢ H.Normal


/-- Prove that there exists a normal subgroup that is not characteristic.-/

theorem exercise_4_4_6b :
  ∃ (G : Type*) (hG : Group G) (H : @Subgroup G hG), @Characteristic G hG H  ∧ ¬ @Normal G hG H :=

⊢ ∃ G hG H, H.Characteristic ∧ ¬H.Normal


/-- If $H$ is the unique subgroup of a given order in a group $G$ prove $H$ is characteristic in $G$.-/

theorem exercise_4_4_7 {G : Type*} [Group G] {H : Subgroup G} [Fintype H]
  (hH : ∀ (K : Subgroup G) (fK : Fintype K), card H = @card K fK → H = K) :
  H.Characteristic :=

G : Type u_1
inst✝¹ : Group G
H : Subgroup G
inst✝ : Fintype ↥H
hH : ∀ (K : Subgroup G) (fK : Fintype ↥K), card ↥H = card ↥K → H = K
⊢ H.Characteristic


/-- Let $G$ be a group with subgroups $H$ and $K$ with $H \leq K$. Prove that if $H$ is characteristic in $K$ and $K$ is normal in $G$ then $H$ is normal in $G$.-/

theorem exercise_4_4_8a {G : Type*} [Group G] (H K : Subgroup G)
  (hHK : H ≤ K) [hHK1 : (H.subgroupOf K).Normal] [hK : K.Normal] :
  H.Normal :=

G : Type u_1
inst✝ : Group G
H K : Subgroup G
hHK : H ≤ K
hHK1 : (H.subgroupOf K).Normal
hK : K.Normal
⊢ H.Normal


/-- Prove that if $P \in \operatorname{Syl}_{p}(G)$ and $H$ is a subgroup of $G$ containing $P$ then $P \in \operatorname{Syl}_{p}(H)$.-/

theorem exercise_4_5_1a {p : ℕ} {G : Type*} [Group G]
  {P : Subgroup G} (hP : IsPGroup p P) (H : Subgroup G)
  (hH : P ≤ H) : IsPGroup p H :=

p : ℕ
G : Type u_1
inst✝ : Group G
P : Subgroup G
hP : IsPGroup p ↥P
H : Subgroup G
hH : P ≤ H
⊢ IsPGroup p ↥H


/-- Prove that a group of order 56 has a normal Sylow $p$-subgroup for some prime $p$ dividing its order.-/

theorem exercise_4_5_13 {G : Type*} [Group G] [Fintype G]
  (hG : card G = 56) :
  ∃ (p : ℕ) (P : Sylow p G), P.Normal :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 56
⊢ ∃ p P, (↑P).Normal


/-- Prove that a group of order 312 has a normal Sylow $p$-subgroup for some prime $p$ dividing its order.-/

theorem exercise_4_5_14 {G : Type*} [Group G] [Fintype G]
  (hG : card G = 312) :
  ∃ (p : ℕ) (P : Sylow p G), P.Normal :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 312
⊢ ∃ p P, (↑P).Normal


/-- Prove that a group of order 351 has a normal Sylow $p$-subgroup for some prime $p$ dividing its order.-/

theorem exercise_4_5_15 {G : Type*} [Group G] [Fintype G]
  (hG : card G = 351) :
  ∃ (p : ℕ) (P : Sylow p G), P.Normal :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 351
⊢ ∃ p P, (↑P).Normal