/-- Let $|G|=p q r$, where $p, q$ and $r$ are primes with $p<q<r$. Prove that $G$ has a normal Sylow subgroup for either $p, q$ or $r$.-/

theorem exercise_4_5_16 {p q r : ℕ} {G : Type*} [Group G]
  [Fintype G]  (hpqr : p < q ∧ q < r)
  (hpqr1 : p.Prime ∧ q.Prime ∧ r.Prime)(hG : card G = p*q*r) :
  Nonempty (Sylow p G) ∨ Nonempty (Sylow q G) ∨ Nonempty (Sylow r G) :=

p q r : ℕ
G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hpqr : p < q ∧ q < r
hpqr1 : p.Prime ∧ q.Prime ∧ r.Prime
hG : card G = p * q * r
⊢ Nonempty (Sylow p G) ∨ Nonempty (Sylow q G) ∨ Nonempty (Sylow r G)


/-- Prove that if $|G|=105$ then $G$ has a normal Sylow 5 -subgroup and a normal Sylow 7-subgroup.-/

theorem exercise_4_5_17 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 105) :
  Nonempty (Sylow 5 G) ∧ Nonempty (Sylow 7 G) :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : card G = 105
⊢ Nonempty (Sylow 5 G) ∧ Nonempty (Sylow 7 G)

import Mathlib

open Fintype Subgroup Set Polynomial Ideal
open scoped BigOperators


/-- Prove that a group of order 200 has a normal Sylow 5-subgroup.-/

theorem exercise_4_5_18 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 200) :
  ∃ N : Sylow 5 G, N.Normal :=

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


/-- Prove that if $|G|=6545$ then $G$ is not simple.-/

theorem exercise_4_5_19 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 6545) : ¬ IsSimpleGroup G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : card G = 6545
⊢ ¬IsSimpleGroup G


/-- Prove that if $|G|=1365$ then $G$ is not simple.-/

theorem exercise_4_5_20 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 1365) : ¬ IsSimpleGroup G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : card G = 1365
⊢ ¬IsSimpleGroup G


/-- Prove that if $|G|=2907$ then $G$ is not simple.-/

theorem exercise_4_5_21 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 2907) : ¬ IsSimpleGroup G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : card G = 2907
⊢ ¬IsSimpleGroup G


/-- Prove that if $|G|=132$ then $G$ is not simple.-/

theorem exercise_4_5_22 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 132) : ¬ IsSimpleGroup G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : card G = 132
⊢ ¬IsSimpleGroup G


/-- Prove that if $|G|=462$ then $G$ is not simple.-/

theorem exercise_4_5_23 {G : Type*} [Fintype G] [Group G]
  (hG : card G = 462) : ¬ IsSimpleGroup G :=

G : Type u_1
inst✝¹ : Fintype G
inst✝ : Group G
hG : card G = 462
⊢ ¬IsSimpleGroup G


/-- Let $G$ be a group of order 105. Prove that if a Sylow 3-subgroup of $G$ is normal then $G$ is abelian.-/

def exercise_4_5_28 {G : Type*} [Group G] [Fintype G]
  (hG : card G = 105) (P : Sylow 3 G) [hP : P.Normal] :
  CommGroup G :=

G : Type u_1
inst✝¹ : Group G
inst✝ : Fintype G
hG : card G = 105
P : Sylow 3 G
hP : (↑P).Normal
⊢ CommGroup G


/-- Let $P$ be a normal Sylow $p$-subgroup of $G$ and let $H$ be any subgroup of $G$. Prove that $P \cap H$ is the unique Sylow $p$-subgroup of $H$.-/

theorem exercise_4_5_33 {G : Type*} [Group G] [Fintype G] {p : ℕ}
  (P : Sylow p G) [hP : P.Normal] (H : Subgroup G) [Fintype H] :
  ∀ R : Sylow p H, R.toSubgroup = (H ⊓ P.toSubgroup).subgroupOf H ∧
  Nonempty (Sylow p H) :=

G : Type u_1
inst✝² : Group G
inst✝¹ : Fintype G
p : ℕ
P : Sylow p G
hP : (↑P).Normal
H : Subgroup G
inst✝ : Fintype ↥H
⊢ ∀ (R : Sylow p ↥H), ↑R = (H ⊓ ↑P).subgroupOf H ∧ Nonempty (Sylow p ↥H)


/-- Prove that a subgroup $H$ of $G$ is normal if and only if $[G, H] \leq H$.-/

theorem exercise_5_4_2 {G : Type*} [Group G] (H : Subgroup G) :
  H.Normal ↔ ⁅(⊤ : Subgroup G), H⁆ ≤ H :=

G : Type u_1
inst✝ : Group G
H : Subgroup G
⊢ H.Normal ↔ ⁅⊤, H⁆ ≤ H


/-- Prove that if $u$ is a unit in $R$ then so is $-u$.-/

theorem exercise_7_1_2 {R : Type*} [Ring R] {u : R}
  (hu : IsUnit u) : IsUnit (-u) :=

R : Type u_1
inst✝ : Ring R
u : R
hu : IsUnit u
⊢ IsUnit (-u)


/-- Prove that if $R$ is an integral domain and $x^{2}=1$ for some $x \in R$ then $x=\pm 1$.-/

theorem exercise_7_1_11 {R : Type*} [Ring R] [IsDomain R]
  {x : R} (hx : x^2 = 1) : x = 1 ∨ x = -1 :=

R : Type u_1
inst✝¹ : Ring R
inst✝ : IsDomain R
x : R
hx : x ^ 2 = 1
⊢ x = 1 ∨ x = -1


/-- Prove that any subring of a field which contains the identity is an integral domain.-/

theorem exercise_7_1_12 {F : Type*} [Field F] {K : Subring F}
  (hK : (1 : F) ∈ K) : IsDomain K :=

F : Type u_1
inst✝ : Field F
K : Subring F
hK : 1 ∈ K
⊢ IsDomain ↥K


/-- A ring $R$ is called a Boolean ring if $a^{2}=a$ for all $a \in R$. Prove that every Boolean ring is commutative.-/

def exercise_7_1_15 {R : Type*} [Ring R] (hR : ∀ a : R, a^2 = a) :
  CommRing R :=

R : Type u_1
inst✝ : Ring R
hR : ∀ (a : R), a ^ 2 = a
⊢ CommRing R


/-- Let $p(x)=a_{n} x^{n}+a_{n-1} x^{n-1}+\cdots+a_{1} x+a_{0}$ be an element of the polynomial ring $R[x]$. Prove that $p(x)$ is a zero divisor in $R[x]$ if and only if there is a nonzero $b \in R$ such that $b p(x)=0$.-/

theorem exercise_7_2_2 {R : Type*} [Ring R] (p : Polynomial R) :
  p ∣ 0 ↔ ∃ b : R, b ≠ 0 ∧ b • p = 0 :=

R : Type u_1
inst✝ : Ring R
p : R[X]
⊢ p ∣ 0 ↔ ∃ b, b ≠ 0 ∧ b • p = 0


/-- Let $G=\left\{g_{1}, \ldots, g_{n}\right\}$ be a finite group. Prove that the element $N=g_{1}+g_{2}+\ldots+g_{n}$ is in the center of the group ring $R G$.-/

theorem exercise_7_2_12 {R G : Type*} [Ring R] [Group G] [Fintype G] :
  ∑ g : G, MonoidAlgebra.of R G g ∈ center (MonoidAlgebra R G) :=

R : Type u_1
G : Type u_2
inst✝² : Ring R
inst✝¹ : Group G
inst✝ : Fintype G
⊢ ∑ g : G, (MonoidAlgebra.of R G) g ∈ Set.center (MonoidAlgebra R G)


/-- Let $\varphi: R \rightarrow S$ be a surjective homomorphism of rings. Prove that the image of the center of $R$ is contained in the center of $S$.-/

theorem exercise_7_3_16 {R S : Type*} [Ring R] [Ring S]
  {φ : R →+* S} (hf : Function.Surjective φ) :
  φ '' (center R) ⊂ center S :=

R : Type u_1
S : Type u_2
inst✝¹ : Ring R
inst✝ : Ring S
φ : R →+* S
hf : Function.Surjective ⇑φ
⊢ ⇑φ '' Set.center R ⊂ Set.center S


/-- An ideal $N$ is called nilpotent if $N^{n}$ is the zero ideal for some $n \geq 1$. Prove that the ideal $p \mathbb{Z} / p^{m} \mathbb{Z}$ is a nilpotent ideal in the ring $\mathbb{Z} / p^{m} \mathbb{Z}$.-/

theorem exercise_7_3_37 {p m : ℕ} (hp : p.Prime) :
  IsNilpotent (span ({↑p} : Set $ ZMod $ p^m) : Ideal $ ZMod $ p^m) :=

p m : ℕ
hp : p.Prime
⊢ IsNilpotent (span {↑p})


/-- Let $R$ be a commutative ring with $1 \neq 0$. Prove that if $a$ is a nilpotent element of $R$ then $1-a b$ is a unit for all $b \in R$.-/

theorem exercise_7_4_27 {R : Type*} [CommRing R] (hR : (0 : R) ≠ 1)
  {a : R} (ha : IsNilpotent a) (b : R) :
  IsUnit (1-a*b) :=

R : Type u_1
inst✝ : CommRing R
hR : 0 ≠ 1
a : R
ha : IsNilpotent a
b : R
⊢ IsUnit (1 - a * b)


/-- Let $N$ be a positive integer. Let $M$ be an integer relatively prime to $N$ and let $d$ be an integer relatively prime to $\varphi(N)$, where $\varphi$ denotes Euler's $\varphi$-function. Prove that if $M_{1} \equiv M^{d} \pmod N$ then $M \equiv M_{1}^{d^{\prime}} \pmod N$ where $d^{\prime}$ is the inverse of $d \bmod \varphi(N)$: $d d^{\prime} \equiv 1 \pmod {\varphi(N)}$.-/

theorem exercise_8_1_12 {N : ℕ} (hN : N > 0) {M M': ℤ} {d : ℕ}
  (hMN : M.gcd N = 1) (hMd : d.gcd N.totient = 1)
  (hM' : M' ≡ M^d [ZMOD N]) :
  ∃ d' : ℕ, d' * d ≡ 1 [ZMOD N.totient] ∧
  M ≡ M'^d' [ZMOD N] :=

N : ℕ
hN : N > 0
M M' : ℤ
d : ℕ
hMN : M.gcd ↑N = 1
hMd : d.gcd N.totient = 1
hM' : M' ≡ M ^ d [ZMOD ↑N]
⊢ ∃ d', ↑d' * ↑d ≡ 1 [ZMOD ↑N.totient] ∧ M ≡ M' ^ d' [ZMOD ↑N]


/-- Let $R$ be an integral domain. Prove that if the following two conditions hold then $R$ is a Principal Ideal Domain: (i) any two nonzero elements $a$ and $b$ in $R$ have a greatest common divisor which can be written in the form $r a+s b$ for some $r, s \in R$, and (ii) if $a_{1}, a_{2}, a_{3}, \ldots$ are nonzero elements of $R$ such that $a_{i+1} \mid a_{i}$ for all $i$, then there is a positive integer $N$ such that $a_{n}$ is a unit times $a_{N}$ for all $n \geq N$.-/

theorem exercise_8_2_4 {R : Type*} [Ring R][NoZeroDivisors R]
  [CancelCommMonoidWithZero R] [GCDMonoid R]
  (h1 : ∀ a b : R, a ≠ 0 → b ≠ 0 → ∃ r s : R, gcd a b = r*a + s*b)
  (h2 : ∀ a : ℕ → R, (∀ i j : ℕ, i < j → a i ∣ a j) →
  ∃ N : ℕ, ∀ n ≥ N, ∃ u : R, IsUnit u ∧ a n = u * a N) :
  IsPrincipalIdealRing R :=

R : Type u_1
inst✝³ : Ring R
inst✝² : NoZeroDivisors R
inst✝¹ : CancelCommMonoidWithZero R
inst✝ : GCDMonoid R
h1 : ∀ (a b : R), a ≠ 0 → b ≠ 0 → ∃ r s, gcd a b = r * a + s * b
h2 : ∀ (a : ℕ → R), (∀ (i j : ℕ), i < j → a i ∣ a j) → ∃ N, ∀ n ≥ N, ∃ u, IsUnit u ∧ a n = u * a N
⊢ IsPrincipalIdealRing R


/-- Prove that if an integer is the sum of two rational squares, then it is the sum of two integer squares.-/

theorem exercise_8_3_4 {R : Type*} {n : ℤ} {r s : ℚ}
  (h : r^2 + s^2 = n) :
  ∃ a b : ℤ, a^2 + b^2 = n :=

R : Type u_1
n : ℤ
r s : ℚ
h : r ^ 2 + s ^ 2 = ↑n
⊢ ∃ a b, a ^ 2 + b ^ 2 = n


/-- Let $R=\mathbb{Z}[\sqrt{-n}]$ where $n$ is a squarefree integer greater than 3. Prove that $2, \sqrt{-n}$ and $1+\sqrt{-n}$ are irreducibles in $R$.-/

theorem exercise_8_3_5a {n : ℤ} (hn0 : n > 3) (hn1 : Squarefree n) :
  Irreducible (2 : Zsqrtd $ -n) ∧
  Irreducible (⟨0, 1⟩ : Zsqrtd $ -n) ∧
  Irreducible (1 + ⟨0, 1⟩ : Zsqrtd $ -n) :=

n : ℤ
hn0 : n > 3
hn1 : Squarefree n
⊢ Irreducible 2 ∧ Irreducible { re := 0, im := 1 } ∧ Irreducible (1 + { re := 0, im := 1 })


/-- Prove that the quotient ring $\mathbb{Z}[i] /(1+i)$ is a field of order 2.-/

theorem exercise_8_3_6a {R : Type} [Ring R]
  (hR : R = (GaussianInt ⧸ span ({⟨0, 1⟩} : Set GaussianInt))) :
  IsField R ∧ ∃ finR : Fintype R, @card R finR = 2 :=

R : Type
inst✝ : Ring R
hR : R = (GaussianInt ⧸ span {{ re := 0, im := 1 }})
⊢ IsField R ∧ ∃ finR, card R = 2


/-- Let $q \in \mathbb{Z}$ be a prime with $q \equiv 3 \bmod 4$. Prove that the quotient ring $\mathbb{Z}[i] /(q)$ is a field with $q^{2}$ elements.-/

theorem exercise_8_3_6b {q : ℕ} (hq0 : q.Prime)
  (hq1 : q ≡ 3 [ZMOD 4]) {R : Type} [Ring R]
  (hR : R = (GaussianInt ⧸ span ({↑q} : Set GaussianInt))) :
  IsField R ∧ ∃ finR : Fintype R, @card R finR = q^2 :=

q : ℕ
hq0 : q.Prime
hq1 : ↑q ≡ 3 [ZMOD 4]
R : Type
inst✝ : Ring R
hR : R = (GaussianInt ⧸ span {↑q})
⊢ IsField R ∧ ∃ finR, card R = q ^ 2


/-- Prove that $(x, y)$ is not a principal ideal in $\mathbb{Q}[x, y]$.-/

theorem exercise_9_1_6 : ¬ Submodule.IsPrincipal
  (span ({MvPolynomial.X 0, MvPolynomial.X 1} : Set (MvPolynomial (Fin 2) ℚ))) :=

⊢ ¬Submodule.IsPrincipal (span {MvPolynomial.X 0, MvPolynomial.X 1})


/-- Prove that the ring $\mathbb{Z}\left[x_{1}, x_{2}, x_{3}, \ldots\right] /\left(x_{1} x_{2}, x_{3} x_{4}, x_{5} x_{6}, \ldots\right)$ contains infinitely many minimal prime ideals.-/

theorem exercise_9_1_10 {f : ℕ → MvPolynomial ℕ ℤ}
  (hf : f = λ i => MvPolynomial.X i * MvPolynomial.X (i+1)):
  Infinite (minimalPrimes (MvPolynomial ℕ ℤ ⧸ span (range f))) :=

f : ℕ → MvPolynomial ℕ ℤ
hf : f = fun i => MvPolynomial.X i * MvPolynomial.X (i + 1)
⊢ Infinite ↑(minimalPrimes (MvPolynomial ℕ ℤ ⧸ span (range f)))


/-- Prove that if $f(x)$ and $g(x)$ are polynomials with rational coefficients whose product $f(x) g(x)$ has integer coefficients, then the product of any coefficient of $g(x)$ with any coefficient of $f(x)$ is an integer.-/

theorem exercise_9_3_2 {f g : Polynomial ℚ} (i j : ℕ)
  (hfg : ∀ n : ℕ, ∃ a : ℤ, (f*g).coeff = a) :
  ∃ a : ℤ, f.coeff i * g.coeff j = a :=

f g : ℚ[X]
i j : ℕ
hfg : ℕ → ∃ a, (f * g).coeff = ↑a
⊢ ∃ a, f.coeff i * g.coeff j = ↑a


/-- Prove that $x^4-4x^3+6$ is irreducible in $\mathbb{Z}[x]$.-/

theorem exercise_9_4_2a : Irreducible (X^4 - 4*X^3 + 6 : Polynomial ℤ) :=


/-- Prove that $x^6+30x^5-15x^3 + 6x-120$ is irreducible in $\mathbb{Z}[x]$.-/

theorem exercise_9_4_2b : Irreducible
  (X^6 + 30*X^5 - 15*X^3 + 6*X - 120 : Polynomial ℤ) :=


/-- Prove that $x^4+4x^3+6x^2+2x+1$ is irreducible in $\mathbb{Z}[x]$.-/

theorem exercise_9_4_2c : Irreducible
  (X^4 + 4*X^3 + 6*X^2 + 2*X + 1 : Polynomial ℤ) :=


/-- Prove that $\frac{(x+2)^p-2^p}{x}$, where $p$ is an odd prime, is irreducible in $\mathbb{Z}[x]$.-/

theorem exercise_9_4_2d {p : ℕ} (hp : p.Prime ∧ p > 2)
  {f : Polynomial ℤ} (hf : f = (X + 2)^p):
  Irreducible (∑ n in (f.support \ {0}), (f.coeff n : Polynomial ℤ) * X ^ (n-1) :
  Polynomial ℤ) :=

p : ℕ
hp : p.Prime ∧ p > 2
f : ℤ[X]
hf : f = (X + 2) ^ p
⊢ Irreducible (∑ n ∈ f.support \ {0}, ↑(f.coeff n) * X ^ (n - 1))


/-- Prove that the polynomial $x^{2}-\sqrt{2}$ is irreducible over $\mathbb{Z}[\sqrt{2}]$. You may assume that $\mathbb{Z}[\sqrt{2}]$ is a U.F.D.-/

theorem exercise_9_4_9 :
  Irreducible (X^2 - C Zsqrtd.sqrtd : Polynomial (Zsqrtd 2)) :=


/-- Prove that $x^2+y^2-1$ is irreducible in $\mathbb{Q}[x,y]$.-/

theorem exercise_9_4_11 :
  Irreducible ((MvPolynomial.X 0)^2 + (MvPolynomial.X 1)^2 - 1 : MvPolynomial (Fin 2) ℚ) :=

⊢ Irreducible (MvPolynomial.X 0 ^ 2 + MvPolynomial.X 1 ^ 2 - 1)


/-- Prove that as vector spaces over $\mathbb{Q}, \mathbb{R}^n \cong \mathbb{R}$, for all $n \in \mathbb{Z}^{+}$.-/

def exercise_11_1_13 {ι : Type*} [Fintype ι] :
  (ι → ℝ) ≃ₗ[ℚ] ℝ :=

ι : Type u_1
inst✝ : Fintype ι
⊢ (ι → ℝ) ≃ₗ[ℚ] ℝ


/-- Let $X$ be a topological space; let $A$ be a subset of $X$. Suppose that for each $x \in A$ there is an open set $U$ containing $x$ such that $U \subset A$. Show that $A$ is open in $X$.-/

theorem exercise_13_1 (X : Type*) [TopologicalSpace X] (A : Set X)
  (h1 : ∀ x ∈ A, ∃ U : Set X, x ∈ U ∧ IsOpen U ∧ U ⊆ A) :
  IsOpen A :=

X : Type u_1
inst✝ : TopologicalSpace X
A : Set X
h1 : ∀ x ∈ A, ∃ U, x ∈ U ∧ IsOpen U ∧ U ⊆ A
⊢ IsOpen A

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology


/-- Show that the collection $$\mathcal{T}_\infty = \{U | X - U \text{ is infinite or empty or all of X}\}$$ does not need to be a topology on the set $X$.-/

theorem exercise_13_3b : ¬ ∀ X : Type, ∀s : Set (Set X),
  (∀ t : Set X, t ∈ s → (Set.Infinite tᶜ ∨ t = ∅ ∨ t = ⊤)) →
  (Set.Infinite (⋃₀ s)ᶜ ∨ (⋃₀ s) = ∅ ∨ (⋃₀ s) = ⊤) :=

⊢ ¬∀ (X : Type) (s : Set (Set X)), (∀ t ∈ s, tᶜ.Infinite ∨ t = ∅ ∨ t = ⊤) → (⋃₀ s)ᶜ.Infinite ∨ ⋃₀ s = ∅ ∨ ⋃₀ s = ⊤


/-- If $\mathcal{T}_\alpha$ is a family of topologies on $X$, show that $\bigcap \mathcal{T}_\alpha$ is a topology on $X$.-/

theorem exercise_13_4a1 (X I : Type*) (T : I → Set (Set X)) (h : ∀ i, is_topology X (T i)) :
  is_topology X (⋂ i : I, T i) :=

X : Type u_1
I : Type u_2
T : I → Set (Set X)
h : ∀ (i : I), is_topology X (T i)
⊢ is_topology X (⋂ i, T i)

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

def is_topology (X : Type*) (T : Set (Set X)) :=
  univ ∈ T ∧
  (∀ s t, s ∈ T → t ∈ T → s ∩ t ∈ T) ∧
  (∀s, (∀t ∈ s, t ∈ T) → sUnion s ∈ T)


/-- If $\mathcal{T}_\alpha$ is a family of topologies on $X$, show that $\bigcup \mathcal{T}_\alpha$ does not need to be a topology on $X$.-/

theorem exercise_13_4a2 :
  ∃ (X I : Type*) (T : I → Set (Set X)),
  (∀ i, is_topology X (T i)) ∧ ¬  is_topology X (⋂ i : I, T i) :=

⊢ ∃ X I T, (∀ (i : I), is_topology X (T i)) ∧ ¬is_topology X (⋂ i, T i)


/-- Let $\mathcal{T}_\alpha$ be a family of topologies on $X$. Show that there is a unique smallest topology on $X$ containing all the collections $\mathcal{T}_\alpha$.-/

theorem exercise_13_4b1 (X I : Type*) (T : I → Set (Set X)) (h : ∀ i, is_topology X (T i)) :
  ∃! T', is_topology X T' ∧ (∀ i, T i ⊆ T') ∧
  ∀ T'', is_topology X T'' → (∀ i, T i ⊆ T'') → T'' ⊆ T' :=

X : Type u_1
I : Type u_2
T : I → Set (Set X)
h : ∀ (i : I), is_topology X (T i)
⊢ ∃! T',
    is_topology X T' ∧
      (∀ (i : I), T i ⊆ T') ∧ ∀ (T'' : Set (Set X)), is_topology X T'' → (∀ (i : I), T i ⊆ T'') → T'' ⊆ T'


/-- Let $\mathcal{T}_\alpha$ be a family of topologies on $X$. Show that there is a unique largest topology on $X$ contained in all the collections $\mathcal{T}_\alpha$.-/

theorem exercise_13_4b2 (X I : Type*) (T : I → Set (Set X)) (h : ∀ i, is_topology X (T i)) :
  ∃! T', is_topology X T' ∧ (∀ i, T' ⊆ T i) ∧
  ∀ T'', is_topology X T'' → (∀ i, T'' ⊆ T i) → T' ⊆ T'' :=

X : Type u_1
I : Type u_2
T : I → Set (Set X)
h : ∀ (i : I), is_topology X (T i)
⊢ ∃! T',
    is_topology X T' ∧
      (∀ (i : I), T' ⊆ T i) ∧ ∀ (T'' : Set (Set X)), is_topology X T'' → (∀ (i : I), T'' ⊆ T i) → T' ⊆ T''


/-- Show that if $\mathcal{A}$ is a basis for a topology on $X$, then the topology generated by $\mathcal{A}$ equals the intersection of all topologies on $X$ that contain $\mathcal{A}$.-/

theorem exercise_13_5a {X : Type*}
  [TopologicalSpace X] (A : Set (Set X)) (hA : IsTopologicalBasis A) :
  generateFrom A = generateFrom (sInter {T | is_topology X T ∧ A ⊆ T}) :=

X : Type u_1
inst✝ : TopologicalSpace X
A : Set (Set X)
hA : IsTopologicalBasis A
⊢ generateFrom A = generateFrom (⋂₀ {T | is_topology X T ∧ A ⊆ T})


/-- Show that if $\mathcal{A}$ is a subbasis for a topology on $X$, then the topology generated by $\mathcal{A}$ equals the intersection of all topologies on $X$ that contain $\mathcal{A}$.-/

theorem exercise_13_5b {X : Type*}
  [t : TopologicalSpace X] (A : Set (Set X)) (hA : t = generateFrom A) :
  generateFrom A = generateFrom (sInter {T | is_topology X T ∧ A ⊆ T}) :=

X : Type u_1
t : TopologicalSpace X
A : Set (Set X)
hA : t = generateFrom A
⊢ generateFrom A = generateFrom (⋂₀ {T | is_topology X T ∧ A ⊆ T})


/-- Show that the lower limit topology $\mathbb{R}_l$ and $K$-topology $\mathbb{R}_K$ are not comparable.-/

theorem exercise_13_6 :
  ¬ (∀ U, Rl.IsOpen U → K_topology.IsOpen U) ∧ ¬ (∀ U, K_topology.IsOpen U → Rl.IsOpen U) :=

⊢ (¬∀ (U : Set ℝ), TopologicalSpace.IsOpen U → TopologicalSpace.IsOpen U) ∧
    ¬∀ (U : Set ℝ), TopologicalSpace.IsOpen U → TopologicalSpace.IsOpen U

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

def lower_limit_topology (X : Type) [Preorder X] :=
  generateFrom {S : Set X | ∃ a b, a < b ∧ S = Ico a b}

def Rl := lower_limit_topology ℝ

def K : Set ℝ := {r | ∃ n : ℕ, r = 1 / n}

def K_topology := generateFrom
  ({S : Set ℝ | ∃ a b, a < b ∧ S = Ioo a b} ∪ {S : Set ℝ | ∃ a b, a < b ∧ S = Ioo a b \ K})


/-- Show that the collection $\{(a,b) \mid a < b, a \text{ and } b \text{ rational}\}$ is a basis that generates the standard topology on $\mathbb{R}$.-/

theorem exercise_13_8a :
  IsTopologicalBasis {S : Set ℝ | ∃ a b : ℚ, a < b ∧ S = Ioo ↑a ↑b} :=

⊢ IsTopologicalBasis {S | ∃ a b, a < b ∧ S = Ioo ↑a ↑b}


/-- Show that the collection $\{(a,b) \mid a < b, a \text{ and } b \text{ rational}\}$ is a basis that generates a topology different from the lower limit topology on $\mathbb{R}$.-/

theorem exercise_13_8b :
  (generateFrom {S : Set ℝ | ∃ a b : ℚ, a < b ∧ S = Ico ↑a ↑b}).IsOpen ≠
  (lower_limit_topology ℝ).IsOpen :=

⊢ TopologicalSpace.IsOpen ≠ TopologicalSpace.IsOpen

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

def lower_limit_topology (X : Type) [Preorder X] :=
  generateFrom {S : Set X | ∃ a b, a < b ∧ S = Ico a b}


/-- Show that if $Y$ is a subspace of $X$, and $A$ is a subset of $Y$, then the topology $A$ inherits as a subspace of $Y$ is the same as the topology it inherits as a subspace of $X$.-/

theorem exercise_16_1 {X : Type*} [TopologicalSpace X]
  (Y : Set X)
  (A : Set Y) :
  ∀ U : Set A, IsOpen U ↔ IsOpen (Subtype.val '' U) :=

X : Type u_1
inst✝ : TopologicalSpace X
Y : Set X
A : Set ↑Y
⊢ ∀ (U : Set ↑A), IsOpen U ↔ IsOpen (Subtype.val '' U)


/-- A map $f: X \rightarrow Y$ is said to be an open map if for every open set $U$ of $X$, the set $f(U)$ is open in $Y$. Show that $\pi_{1}: X \times Y \rightarrow X$ and $\pi_{2}: X \times Y \rightarrow Y$ are open maps.-/

theorem exercise_16_4 {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y]
  (π₁ : X × Y → X)
  (π₂ : X × Y → Y)
  (h₁ : π₁ = Prod.fst)
  (h₂ : π₂ = Prod.snd) :
  IsOpenMap π₁ ∧ IsOpenMap π₂ :=

X : Type u_1
Y : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : TopologicalSpace Y
π₁ : X × Y → X
π₂ : X × Y → Y
h₁ : π₁ = Prod.fst
h₂ : π₂ = Prod.snd
⊢ IsOpenMap π₁ ∧ IsOpenMap π₂


/-- Show that the countable collection \[\{(a, b) \times (c, d) \mid a < b \text{ and } c < d, \text{ and } a, b, c, d \text{ are rational}\}\] is a basis for $\mathbb{R}^2$.-/

theorem exercise_16_6
  (S : Set (Set (ℝ × ℝ)))
  (hS : ∀ s, s ∈ S → ∃ a b c d, (rational a ∧ rational b ∧ rational c ∧ rational d
  ∧ s = {x | ∃ x₁ x₂, x = (x₁, x₂) ∧ a < x₁ ∧ x₁ < b ∧ c < x₂ ∧ x₂ < d})) :
  IsTopologicalBasis S :=

S : Set (Set (ℝ × ℝ))
hS :
  ∀ s ∈ S,
    ∃ a b c d,
      rational a ∧
        rational b ∧ rational c ∧ rational d ∧ s = {x | ∃ x₁ x₂, x = (x₁, x₂) ∧ a < x₁ ∧ x₁ < b ∧ c < x₂ ∧ x₂ < d}
⊢ IsTopologicalBasis S

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

def rational (x : ℝ) := x ∈ range ((↑) : ℚ → ℝ)


/-- Show that if $U$ is open in $X$ and $A$ is closed in $X$, then $U-A$ is open in $X$, and $A-U$ is closed in $X$.-/

theorem exercise_17_4 {X : Type*} [TopologicalSpace X]
  (U A : Set X) (hU : IsOpen U) (hA : IsClosed A) :
  IsOpen (U \ A) ∧ IsClosed (A \ U) :=

X : Type u_1
inst✝ : TopologicalSpace X
U A : Set X
hU : IsOpen U
hA : IsClosed A
⊢ IsOpen (U \ A) ∧ IsClosed (A \ U)


/-- Let $Y$ be an ordered set in the order topology. Let $f, g: X \rightarrow Y$ be continuous. Show that the set $\{x \mid f(x) \leq g(x)\}$ is closed in $X$.-/

theorem exercise_18_8a {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y]
  [LinearOrder Y] [OrderTopology Y] {f g : X → Y}
  (hf : Continuous f) (hg : Continuous g) :
  IsClosed {x | f x ≤ g x} :=

X : Type u_1
Y : Type u_2
inst✝³ : TopologicalSpace X
inst✝² : TopologicalSpace Y
inst✝¹ : LinearOrder Y
inst✝ : OrderTopology Y
f g : X → Y
hf : Continuous f
hg : Continuous g
⊢ IsClosed {x | f x ≤ g x}


/-- Let $Y$ be an ordered set in the order topology. Let $f, g: X \rightarrow Y$ be continuous. Let $h: X \rightarrow Y$ be the function $h(x)=\min \{f(x), g(x)\}.$ Show that $h$ is continuous.-/

theorem exercise_18_8b {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y]
  [LinearOrder Y] [OrderTopology Y] {f g : X → Y}
  (hf : Continuous f) (hg : Continuous g) :
  Continuous (λ x => min (f x) (g x)) :=

X : Type u_1
Y : Type u_2
inst✝³ : TopologicalSpace X
inst✝² : TopologicalSpace Y
inst✝¹ : LinearOrder Y
inst✝ : OrderTopology Y
f g : X → Y
hf : Continuous f
hg : Continuous g
⊢ Continuous fun x => min (f x) (g x)


/-- Let $A \subset X$; let $f: A \rightarrow Y$ be continuous; let $Y$ be Hausdorff. Show that if $f$ may be extended to a continuous function $g: \bar{A} \rightarrow Y$, then $g$ is uniquely determined by $f$.-/

theorem exercise_18_13
  {X : Type*} [TopologicalSpace X] {Y : Type*} [TopologicalSpace Y]
  [T2Space Y] {A : Set X} {f : A → Y} (hf : Continuous f)
  (g : closure A → Y)
  (g_con : Continuous g) :
  ∀ (g' : closure A → Y), Continuous g' →  (∀ (x : closure A), g x = g' x) :=

X : Type u_1
inst✝² : TopologicalSpace X
Y : Type u_2
inst✝¹ : TopologicalSpace Y
inst✝ : T2Space Y
A : Set X
f : ↑A → Y
hf : Continuous f
g : ↑(closure A) → Y
g_con : Continuous g
⊢ ∀ (g' : ↑(closure A) → Y), Continuous g' → ∀ (x : ↑(closure A)), g x = g' x


/-- Let $\mathbf{x}_1, \mathbf{x}_2, \ldots$ be a sequence of the points of the product space $\prod X_\alpha$.  Show that this sequence converges to the point $\mathbf{x}$ if and only if the sequence $\pi_\alpha(\mathbf{x}_i)$ converges to $\pi_\alpha(\mathbf{x})$ for each $\alpha$.-/

theorem exercise_19_6a
  {n : ℕ}
  {f : Fin n → Type*} {x : ℕ → Πa, f a}
  (y : Πi, f i)
  [Πa, TopologicalSpace (f a)] :
  Tendsto x atTop (𝓝 y) ↔ ∀ i, Tendsto (λ j => (x j) i) atTop (𝓝 (y i)) :=

n : ℕ
f : Fin n → Type u_1
x : ℕ → (a : Fin n) → f a
y : (i : Fin n) → f i
inst✝ : (a : Fin n) → TopologicalSpace (f a)
⊢ Tendsto x atTop (𝓝 y) ↔ ∀ (i : Fin n), Tendsto (fun j => x j i) atTop (𝓝 (y i))


/-- Show that $\mathbb{R} \times \mathbb{R}$ in the dictionary order topology is metrizable.-/

theorem exercise_20_2
  [TopologicalSpace (ℝ ×ₗ ℝ)] [OrderTopology (ℝ ×ₗ ℝ)]
  : MetrizableSpace (ℝ ×ₗ ℝ) :=

inst✝¹ : TopologicalSpace (Lex (ℝ × ℝ))
inst✝ : OrderTopology (Lex (ℝ × ℝ))
⊢ MetrizableSpace (Lex (ℝ × ℝ))


/-- Define $f_{n}:[0,1] \rightarrow \mathbb{R}$ by the equation $f_{n}(x)=x^{n}$. Show that the sequence $\left(f_{n}(x)\right)$ converges for each $x \in[0,1]$.-/

theorem exercise_21_6a
  (f : ℕ → I → ℝ )
  (h : ∀ x n, f n x = x ^ n) :
  ∀ x, ∃ y, Tendsto (λ n => f n x) atTop (𝓝 y) :=

f : ℕ → ↑I → ℝ
h : ∀ (x : ↑I) (n : ℕ), f n x = ↑x ^ n
⊢ ∀ (x : ↑I), ∃ y, Tendsto (fun n => f n x) atTop (𝓝 y)

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

abbrev I : Set ℝ := Icc 0 1


/-- Define $f_{n}:[0,1] \rightarrow \mathbb{R}$ by the equation $f_{n}(x)=x^{n}$. Show that the sequence $\left(f_{n}\right)$ does not converge uniformly.-/

theorem exercise_21_6b
  (f : ℕ → I → ℝ )
  (h : ∀ x n, f n x = x ^ n) :
  ¬ ∃ f₀, TendstoUniformly f f₀ atTop :=

f : ℕ → ↑I → ℝ
h : ∀ (x : ↑I) (n : ℕ), f n x = ↑x ^ n
⊢ ¬∃ f₀, TendstoUniformly f f₀ atTop


/-- Let $X$ be a topological space and let $Y$ be a metric space. Let $f_{n}: X \rightarrow Y$ be a sequence of continuous functions. Let $x_{n}$ be a sequence of points of $X$ converging to $x$. Show that if the sequence $\left(f_{n}\right)$ converges uniformly to $f$, then $\left(f_{n}\left(x_{n}\right)\right)$ converges to $f(x)$.-/

theorem exercise_21_8
  {X : Type*} [TopologicalSpace X] {Y : Type*} [MetricSpace Y]
  {f : ℕ → X → Y} {x : ℕ → X}
  (hf : ∀ n, Continuous (f n))
  (x₀ : X)
  (hx : Tendsto x atTop (𝓝 x₀))
  (f₀ : X → Y)
  (hh : TendstoUniformly f f₀ atTop) :
  Tendsto (λ n => f n (x n)) atTop (𝓝 (f₀ x₀)) :=

X : Type u_1
inst✝¹ : TopologicalSpace X
Y : Type u_2
inst✝ : MetricSpace Y
f : ℕ → X → Y
x : ℕ → X
hf : ∀ (n : ℕ), Continuous (f n)
x₀ : X
hx : Tendsto x atTop (𝓝 x₀)
f₀ : X → Y
hh : TendstoUniformly f f₀ atTop
⊢ Tendsto (fun n => f n (x n)) atTop (𝓝 (f₀ x₀))


/-- Let $p: X \rightarrow Y$ be a continuous map. Show that if there is a continuous map $f: Y \rightarrow X$ such that $p \circ f$ equals the identity map of $Y$, then $p$ is a quotient map.-/

theorem exercise_22_2a {X Y : Type*} [TopologicalSpace X]
  [TopologicalSpace Y] (p : X → Y) (h : Continuous p) :
  QuotientMap p ↔ ∃ (f : Y → X), Continuous f ∧ p ∘ f = id :=

X : Type u_1
Y : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : TopologicalSpace Y
p : X → Y
h : Continuous p
⊢ QuotientMap p ↔ ∃ f, Continuous f ∧ p ∘ f = id


/-- If $A \subset X$, a retraction of $X$ onto $A$ is a continuous map $r: X \rightarrow A$ such that $r(a)=a$ for each $a \in A$. Show that a retraction is a quotient map.-/

theorem exercise_22_2b {X : Type*} [TopologicalSpace X]
  {A : Set X} (r : X → A) (hr : Continuous r) (h : ∀ x : A, r x = x) :
  QuotientMap r :=

X : Type u_1
inst✝ : TopologicalSpace X
A : Set X
r : X → ↑A
hr : Continuous r
h : ∀ (x : ↑A), r ↑x = x
⊢ QuotientMap r


/-- Let $p \colon X \rightarrow Y$ be an open map. Show that if $A$ is open in $X$, then the map $q \colon A \rightarrow p(A)$ obtained by restricting $p$ is an open map.-/

theorem exercise_22_5 {X Y : Type*} [TopologicalSpace X]
  [TopologicalSpace Y] (p : X → Y) (hp : IsOpenMap p)
  (A : Set X) (hA : IsOpen A) : IsOpenMap (p ∘ Subtype.val : A → Y) :=

X : Type u_1
Y : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : TopologicalSpace Y
p : X → Y
hp : IsOpenMap p
A : Set X
hA : IsOpen A
⊢ IsOpenMap (p ∘ Subtype.val)


/-- Let $\left\{A_{n}\right\}$ be a sequence of connected subspaces of $X$, such that $A_{n} \cap A_{n+1} \neq \varnothing$ for all $n$. Show that $\bigcup A_{n}$ is connected.-/

theorem exercise_23_2 {X : Type*}
  [TopologicalSpace X] {A : ℕ → Set X} (hA : ∀ n, IsConnected (A n))
  (hAn : ∀ n, A n ∩ A (n + 1) ≠ ∅) :
  IsConnected (⋃ n, A n) :=

X : Type u_1
inst✝ : TopologicalSpace X
A : ℕ → Set X
hA : ∀ (n : ℕ), IsConnected (A n)
hAn : ∀ (n : ℕ), A n ∩ A (n + 1) ≠ ∅
⊢ IsConnected (⋃ n, A n)


/-- Let $\left\{A_{\alpha}\right\}$ be a collection of connected subspaces of $X$; let $A$ be a connected subset of $X$. Show that if $A \cap A_{\alpha} \neq \varnothing$ for all $\alpha$, then $A \cup\left(\bigcup A_{\alpha}\right)$ is connected.-/

theorem exercise_23_3 {X : Type*} [TopologicalSpace X]
  [TopologicalSpace X] {A : ℕ → Set X}
  (hAn : ∀ n, IsConnected (A n))
  (A₀ : Set X)
  (hA : IsConnected A₀)
  (h : ∀ n, A₀ ∩ A n ≠ ∅) :
  IsConnected (A₀ ∪ (⋃ n, A n)) :=

X : Type u_1
inst✝¹ inst✝ : TopologicalSpace X
A : ℕ → Set X
hAn : ∀ (n : ℕ), IsConnected (A n)
A₀ : Set X
hA : IsConnected A₀
h : ∀ (n : ℕ), A₀ ∩ A n ≠ ∅
⊢ IsConnected (A₀ ∪ ⋃ n, A n)


/-- Show that if $X$ is an infinite set, it is connected in the finite complement topology.-/

theorem exercise_23_4 {X : Type*} [TopologicalSpace X] [CofiniteTopology X]
  (s : Set X) : Infinite s → IsConnected s :=

X : Type u_1
inst✝¹ : TopologicalSpace X
inst✝ : CofiniteTopology X
s : Set X
⊢ Infinite ↑s → IsConnected s

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

set_option checkBinderAnnotations false


/-- Let $A \subset X$. Show that if $C$ is a connected subspace of $X$ that intersects both $A$ and $X-A$, then $C$ intersects $\operatorname{Bd} A$.-/

theorem exercise_23_6 {X : Type*}
  [TopologicalSpace X] {A C : Set X} (hc : IsConnected C)
  (hCA : C ∩ A ≠ ∅) (hCXA : C ∩ Aᶜ ≠ ∅) :
  C ∩ (frontier A) ≠ ∅ :=

X : Type u_1
inst✝ : TopologicalSpace X
A C : Set X
hc : IsConnected C
hCA : C ∩ A ≠ ∅
hCXA : C ∩ Aᶜ ≠ ∅
⊢ C ∩ frontier A ≠ ∅


/-- Let $A$ be a proper subset of $X$, and let $B$ be a proper subset of $Y$. If $X$ and $Y$ are connected, show that $(X \times Y)-(A \times B)$ is connected.-/

theorem exercise_23_9 {X Y : Type*}
  [TopologicalSpace X] [TopologicalSpace Y]
  (A₁ A₂ : Set X)
  (B₁ B₂ : Set Y)
  (hA : A₁ ⊂ A₂)
  (hB : B₁ ⊂ B₂)
  (hA : IsConnected A₂)
  (hB : IsConnected B₂) :
  IsConnected ({x | ∃ a b, x = (a, b) ∧ a ∈ A₂ ∧ b ∈ B₂} \
      {x | ∃ a b, x = (a, b) ∧ a ∈ A₁ ∧ b ∈ B₁}) :=

X : Type u_1
Y : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : TopologicalSpace Y
A₁ A₂ : Set X
B₁ B₂ : Set Y
hA✝ : A₁ ⊂ A₂
hB✝ : B₁ ⊂ B₂
hA : IsConnected A₂
hB : IsConnected B₂
⊢ IsConnected ({x | ∃ a b, x = (a, b) ∧ a ∈ A₂ ∧ b ∈ B₂} \ {x | ∃ a b, x = (a, b) ∧ a ∈ A₁ ∧ b ∈ B₁})


/-- Let $p: X \rightarrow Y$ be a quotient map. Show that if each set $p^{-1}(\{y\})$ is connected, and if $Y$ is connected, then $X$ is connected.-/

theorem exercise_23_11 {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y]
  (p : X → Y) (hq : QuotientMap p)
  (hY : ConnectedSpace Y) (hX : ∀ y : Y, IsConnected (p ⁻¹' {y})) :
  ConnectedSpace X :=

X : Type u_1
Y : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : TopologicalSpace Y
p : X → Y
hq : QuotientMap p
hY : ConnectedSpace Y
hX : ∀ (y : Y), IsConnected (p ⁻¹' {y})
⊢ ConnectedSpace X


/-- Let $f: S^{1} \rightarrow \mathbb{R}$ be a continuous map. Show there exists a point $x$ of $S^{1}$ such that $f(x)=f(-x)$.-/

theorem exercise_24_2 {f : (Metric.sphere 0 1 : Set ℝ) → ℝ}
  (hf : Continuous f) : ∃ x, f x = f (-x) :=

f : ↑(Metric.sphere 0 1) → ℝ
hf : Continuous f
⊢ ∃ x, f x = f (-x)


/-- Let $f \colon X \rightarrow X$ be continuous. Show that if $X = [0, 1]$, there is a point $x$ such that $f(x) = x$. (The point $x$ is called a fixed point of $f$.)-/

theorem exercise_24_3a [TopologicalSpace I] [CompactSpace I]
  (f : I → I) (hf : Continuous f) :
  ∃ (x : I), f x = x :=

I : Type u_1
inst✝¹ : TopologicalSpace I
inst✝ : CompactSpace I
f : I → I
hf : Continuous f
⊢ ∃ x, f x = x


/-- Let $X$ be locally path connected. Show that every connected open set in $X$ is path connected.-/

theorem exercise_25_4 {X : Type*} [TopologicalSpace X]
  [LocPathConnectedSpace X] (U : Set X) (hU : IsOpen U)
  (hcU : IsConnected U) : IsPathConnected U :=

X : Type u_1
inst✝¹ : TopologicalSpace X
inst✝ : LocPathConnectedSpace X
U : Set X
hU : IsOpen U
hcU : IsConnected U
⊢ IsPathConnected U


/-- Let $G$ be a topological group; let $C$ be the component of $G$ containing the identity element $e$. Show that $C$ is a normal subgroup of $G$.-/

theorem exercise_25_9 {G : Type*} [TopologicalSpace G] [Group G]
  [TopologicalGroup G] (C : Set G) (h : C = connectedComponent 1) :
  IsNormalSubgroup C :=

G : Type u_1
inst✝² : TopologicalSpace G
inst✝¹ : Group G
inst✝ : TopologicalGroup G
C : Set G
h : C = connectedComponent 1
⊢ IsNormalSubgroup C


/-- Let $X$ be a compact Hausdorff space. Let $\mathcal{A}$ be a collection of closed connected subsets of $X$ that is simply ordered by proper inclusion. Then $Y=\bigcap_{A \in \mathcal{A}} A$ is connected.-/

theorem exercise_26_11
  {X : Type*} [TopologicalSpace X] [CompactSpace X] [T2Space X]
  (A : Set (Set X)) (hA : ∀ (a b : Set X), a ∈ A → b ∈ A → a ⊆ b ∨ b ⊆ a)
  (hA' : ∀ a ∈ A, IsClosed a) (hA'' : ∀ a ∈ A, IsConnected a) :
  IsConnected (⋂₀ A) :=

X : Type u_1
inst✝² : TopologicalSpace X
inst✝¹ : CompactSpace X
inst✝ : T2Space X
A : Set (Set X)
hA : ∀ (a b : Set X), a ∈ A → b ∈ A → a ⊆ b ∨ b ⊆ a
hA' : ∀ a ∈ A, IsClosed a
hA'' : ∀ a ∈ A, IsConnected a
⊢ IsConnected (⋂₀ A)


/-- Let $p: X \rightarrow Y$ be a closed continuous surjective map such that $p^{-1}(\{y\})$ is compact, for each $y \in Y$. (Such a map is called a perfect map.) Show that if $Y$ is compact, then $X$ is compact.-/

theorem exercise_26_12 {X Y : Type*} [TopologicalSpace X] [TopologicalSpace Y]
  (p : X → Y) (h : Function.Surjective p) (hc : Continuous p) (hp : ∀ y, IsCompact (p ⁻¹' {y}))
  (hY : CompactSpace Y) : CompactSpace X :=

X : Type u_1
Y : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : TopologicalSpace Y
p : X → Y
h : Function.Surjective p
hc : Continuous p
hp : ∀ (y : Y), IsCompact (p ⁻¹' {y})
hY : CompactSpace Y
⊢ CompactSpace X


/-- Show that a connected metric space having more than one point is uncountable.-/

theorem exercise_27_4
  {X : Type*} [MetricSpace X] [ConnectedSpace X] (hX : ∃ x y : X, x ≠ y) :
  ¬ Countable (univ : Set X) :=

X : Type u_1
inst✝¹ : MetricSpace X
inst✝ : ConnectedSpace X
hX : ∃ x y, x ≠ y
⊢ ¬Countable ↑univ


/-- A space $X$ is said to be countably compact if every countable open covering of $X$ contains a finite subcollection that covers $X$. Show that for a $T_1$ space $X$, countable compactness is equivalent to limit point compactness.-/

theorem exercise_28_4 {X : Type*}
  [TopologicalSpace X] (hT1 : T1Space X) :
  countably_compact X ↔ limit_point_compact X :=

X : Type u_1
inst✝ : TopologicalSpace X
hT1 : T1Space X
⊢ countably_compact X ↔ limit_point_compact X

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

def countably_compact (X : Type*) [TopologicalSpace X] :=
  ∀ U : ℕ → Set X,
  (∀ i, IsOpen (U i)) ∧ ((univ : Set X) ⊆ ⋃ i, U i) →
  (∃ t : Finset ℕ, (univ : Set X) ⊆ ⋃ i ∈ t, U i)

def limit_point_compact (X : Type*) [TopologicalSpace X] :=
  ∀ U : Set X, Infinite U → ∃ x ∈ U, ClusterPt x (𝓟 U)


/-- Show that X is countably compact if and only if every nested sequence $C_1 \supset C_2 \supset \cdots$ of closed nonempty sets of X has a nonempty intersection.-/

theorem exercise_28_5
  (X : Type*) [TopologicalSpace X] :
  countably_compact X ↔ ∀ (C : ℕ → Set X), (∀ n, IsClosed (C n)) ∧
  (∀ n, C n ≠ ∅) ∧ (∀ n, C n ⊆ C (n + 1)) → ∃ x, ∀ n, x ∈ C n :=

X : Type u_1
inst✝ : TopologicalSpace X
⊢ countably_compact X ↔
    ∀ (C : ℕ → Set X),
      ((∀ (n : ℕ), IsClosed (C n)) ∧ (∀ (n : ℕ), C n ≠ ∅) ∧ ∀ (n : ℕ), C n ⊆ C (n + 1)) → ∃ x, ∀ (n : ℕ), x ∈ C n

import Mathlib

open Filter Set TopologicalSpace
open scoped Topology

def countably_compact (X : Type*) [TopologicalSpace X] :=
  ∀ U : ℕ → Set X,
  (∀ i, IsOpen (U i)) ∧ ((univ : Set X) ⊆ ⋃ i, U i) →
  (∃ t : Finset ℕ, (univ : Set X) ⊆ ⋃ i ∈ t, U i)


/-- Let $(X, d)$ be a metric space. If $f: X \rightarrow X$ satisfies the condition $d(f(x), f(y))=d(x, y)$ for all $x, y \in X$, then $f$ is called an isometry of $X$. Show that if $f$ is an isometry and $X$ is compact, then $f$ is bijective and hence a homeomorphism.-/

theorem exercise_28_6 {X : Type*} [MetricSpace X]
  [CompactSpace X] {f : X → X} (hf : Isometry f) :
  Function.Bijective f :=

X : Type u_1
inst✝¹ : MetricSpace X
inst✝ : CompactSpace X
f : X → X
hf : Isometry f
⊢ Function.Bijective f


/-- Show that the rationals $\mathbb{Q}$ are not locally compact.-/

theorem exercise_29_1 : ¬ LocallyCompactSpace ℚ :=


/-- Show that $[0, 1]^\omega$ is not locally compact in the uniform topology.-/

theorem exercise_29_4 [TopologicalSpace (ℕ → I)] :
  ¬ LocallyCompactSpace (ℕ → I) :=

inst✝ : TopologicalSpace (ℕ → ↑I)
⊢ ¬LocallyCompactSpace (ℕ → ↑I)


/-- Show that if $X$ is a Hausdorff space that is locally compact at the point $x$, then for each neighborhood $U$ of $x$, there is a neighborhood $V$ of $x$ such that $\bar{V}$ is compact and $\bar{V} \subset U$.-/

theorem exercise_29_10 {X : Type*}
  [TopologicalSpace X] [T2Space X] (x : X)
  (hx : ∃ U : Set X, x ∈ U ∧ IsOpen U ∧ (∃ K : Set X, U ⊂ K ∧ IsCompact K))
  (U : Set X) (hU : IsOpen U) (hxU : x ∈ U) :
  ∃ (V : Set X), IsOpen V ∧ x ∈ V ∧ IsCompact (closure V) ∧ closure V ⊆ U :=

X : Type u_1
inst✝¹ : TopologicalSpace X
inst✝ : T2Space X
x : X
hx : ∃ U, x ∈ U ∧ IsOpen U ∧ ∃ K, U ⊂ K ∧ IsCompact K
U : Set X
hU : IsOpen U
hxU : x ∈ U
⊢ ∃ V, IsOpen V ∧ x ∈ V ∧ IsCompact (closure V) ∧ closure V ⊆ U


/-- Show that if $X$ is a countable product of spaces having countable dense subsets, then $X$ has a countable dense subset.-/

theorem exercise_30_10
  {X : ℕ → Type*} [∀ i, TopologicalSpace (X i)]
  (h : ∀ i, ∃ (s : Set (X i)), Countable s ∧ Dense s) :
  ∃ (s : Set (Π i, X i)), Countable s ∧ Dense s :=

X : ℕ → Type u_1
inst✝ : (i : ℕ) → TopologicalSpace (X i)
h : ∀ (i : ℕ), ∃ s, Countable ↑s ∧ Dense s
⊢ ∃ s, Countable ↑s ∧ Dense s


/-- Show that if $X$ has a countable dense subset, every collection of disjoint open sets in $X$ is countable.-/

theorem exercise_30_13 {X : Type*} [TopologicalSpace X]
  (h : ∃ (s : Set X), Countable s ∧ Dense s) (U : Set (Set X))
  (hU : ∀ (x y : Set X), x ∈ U → y ∈ U → x ≠ y → x ∩ y = ∅) :
  Countable U :=

X : Type u_1
inst✝ : TopologicalSpace X
h : ∃ s, Countable ↑s ∧ Dense s
U : Set (Set X)
hU : ∀ (x y : Set X), x ∈ U → y ∈ U → x ≠ y → x ∩ y = ∅
⊢ Countable ↑U


/-- Show that if $X$ is regular, every pair of points of $X$ have neighborhoods whose closures are disjoint.-/

theorem exercise_31_1 {X : Type*} [TopologicalSpace X]
  (hX : RegularSpace X) (x y : X) :
  ∃ (U V : Set X), IsOpen U ∧ IsOpen V ∧ x ∈ U ∧ y ∈ V ∧ closure U ∩ closure V = ∅ :=

X : Type u_1
inst✝ : TopologicalSpace X
hX : RegularSpace X
x y : X
⊢ ∃ U V, IsOpen U ∧ IsOpen V ∧ x ∈ U ∧ y ∈ V ∧ closure U ∩ closure V = ∅


/-- Show that if $X$ is normal, every pair of disjoint closed sets have neighborhoods whose closures are disjoint.-/

theorem exercise_31_2 {X : Type*}
  [TopologicalSpace X] [NormalSpace X] {A B : Set X}
  (hA : IsClosed A) (hB : IsClosed B) (hAB : Disjoint A B) :
  ∃ (U V : Set X), IsOpen U ∧ IsOpen V ∧ A ⊆ U ∧ B ⊆ V ∧ closure U ∩ closure V = ∅ :=

X : Type u_1
inst✝¹ : TopologicalSpace X
inst✝ : NormalSpace X
A B : Set X
hA : IsClosed A
hB : IsClosed B
hAB : Disjoint A B
⊢ ∃ U V, IsOpen U ∧ IsOpen V ∧ A ⊆ U ∧ B ⊆ V ∧ closure U ∩ closure V = ∅


/-- Show that every order topology is regular.-/

theorem exercise_31_3 {α : Type*} [PartialOrder α]
  [TopologicalSpace α] (h : OrderTopology α) : RegularSpace α :=

α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : TopologicalSpace α
h : OrderTopology α
⊢ RegularSpace α


/-- Show that a closed subspace of a normal space is normal.-/

theorem exercise_32_1 {X : Type*} [TopologicalSpace X]
  (hX : NormalSpace X) (A : Set X) (hA : IsClosed A) :
  NormalSpace {x // x ∈ A} :=

X : Type u_1
inst✝ : TopologicalSpace X
hX : NormalSpace X
A : Set X
hA : IsClosed A
⊢ NormalSpace { x // x ∈ A }


/-- Show that if $\prod X_\alpha$ is Hausdorff, then so is $X_\alpha$. Assume that each $X_\alpha$ is nonempty.-/

theorem exercise_32_2a
  {ι : Type*} {X : ι → Type*} [∀ i, TopologicalSpace (X i)]
  (h : ∀ i, Nonempty (X i)) (h2 : T2Space (Π i, X i)) :
  ∀ i, T2Space (X i) :=

ι : Type u_1
X : ι → Type u_2
inst✝ : (i : ι) → TopologicalSpace (X i)
h : ∀ (i : ι), Nonempty (X i)
h2 : T2Space ((i : ι) → X i)
⊢ ∀ (i : ι), T2Space (X i)


/-- Show that if $\prod X_\alpha$ is regular, then so is $X_\alpha$. Assume that each $X_\alpha$ is nonempty.-/

theorem exercise_32_2b
  {ι : Type*} {X : ι → Type*} [∀ i, TopologicalSpace (X i)]
  (h : ∀ i, Nonempty (X i)) (h2 : RegularSpace (Π i, X i)) :
  ∀ i, RegularSpace (X i) :=

ι : Type u_1
X : ι → Type u_2
inst✝ : (i : ι) → TopologicalSpace (X i)
h : ∀ (i : ι), Nonempty (X i)
h2 : RegularSpace ((i : ι) → X i)
⊢ ∀ (i : ι), RegularSpace (X i)


/-- Show that if $\prod X_\alpha$ is normal, then so is $X_\alpha$. Assume that each $X_\alpha$ is nonempty.-/

theorem exercise_32_2c
  {ι : Type*} {X : ι → Type*} [∀ i, TopologicalSpace (X i)]
  (h : ∀ i, Nonempty (X i)) (h2 : NormalSpace (Π i, X i)) :
  ∀ i, NormalSpace (X i) :=

ι : Type u_1
X : ι → Type u_2
inst✝ : (i : ι) → TopologicalSpace (X i)
h : ∀ (i : ι), Nonempty (X i)
h2 : NormalSpace ((i : ι) → X i)
⊢ ∀ (i : ι), NormalSpace (X i)


/-- Show that every locally compact Hausdorff space is regular.-/

theorem exercise_32_3 {X : Type*} [TopologicalSpace X]
  (hX : LocallyCompactSpace X) (hX' : T2Space X) :
  RegularSpace X :=

X : Type u_1
inst✝ : TopologicalSpace X
hX : LocallyCompactSpace X
hX' : T2Space X
⊢ RegularSpace X


/-- Show that every locally compact Hausdorff space is completely regular.-/

theorem exercise_33_7 {X : Type*} [TopologicalSpace X]
  (hX : LocallyCompactSpace X) (hX' : T2Space X) :
  ∀ x A, IsClosed A ∧ ¬ x ∈ A →
  ∃ (f : X → I), Continuous f ∧ f x = 1 ∧ f '' A = {0} :=

X : Type u_1
inst✝ : TopologicalSpace X
hX : LocallyCompactSpace X
hX' : T2Space X
⊢ ∀ (x : X) (A : Set X), IsClosed A ∧ x ∉ A → ∃ f, Continuous f ∧ f x = 1 ∧ f '' A = {0}


/-- Let $X$ be completely regular, let $A$ and $B$ be disjoint closed subsets of $X$. Show that if $A$ is compact, there is a continuous function $f \colon X \rightarrow [0, 1]$ such that $f(A) = \{0\}$ and $f(B) = \{1\}$.-/

theorem exercise_33_8
  (X : Type*) [TopologicalSpace X] [RegularSpace X]
  (h : ∀ x A, IsClosed A ∧ ¬ x ∈ A →
  ∃ (f : X → I), Continuous f ∧ f x = (1 : I) ∧ f '' A = {0})
  (A B : Set X) (hA : IsClosed A) (hB : IsClosed B)
  (hAB : Disjoint A B)
  (hAc : IsCompact A) :
  ∃ (f : X → I), Continuous f ∧ f '' A = {0} ∧ f '' B = {1} :=

X : Type u_1
inst✝¹ : TopologicalSpace X
inst✝ : RegularSpace X
h : ∀ (x : X) (A : Set X), IsClosed A ∧ x ∉ A → ∃ f, Continuous f ∧ f x = 1 ∧ f '' A = {0}
A B : Set X
hA : IsClosed A
hB : IsClosed B
hAB : Disjoint A B
hAc : IsCompact A
⊢ ∃ f, Continuous f ∧ f '' A = {0} ∧ f '' B = {1}


/-- Let $X$ be a compact Hausdorff space that is the union of the closed subspaces $X_1$ and $X_2$. If $X_1$ and $X_2$ are metrizable, show that $X$ is metrizable.-/

theorem exercise_34_9
  (X : Type*) [TopologicalSpace X] [CompactSpace X]
  (X1 X2 : Set X) (hX1 : IsClosed X1) (hX2 : IsClosed X2)
  (hX : X1 ∪ X2 = univ) (hX1m : MetrizableSpace X1)
  (hX2m : MetrizableSpace X2) : MetrizableSpace X :=

X : Type u_1
inst✝¹ : TopologicalSpace X
inst✝ : CompactSpace X
X1 X2 : Set X
hX1 : IsClosed X1
hX2 : IsClosed X2
hX : X1 ∪ X2 = univ
hX1m : MetrizableSpace ↑X1
hX2m : MetrizableSpace ↑X2
⊢ MetrizableSpace X


/-- Let $X$ be completely regular. Show that $X$ is connected if and only if the Stone-Čech compactification of $X$ is connected.-/

theorem exercise_38_6 {X : Type*}
  (X : Type*) [TopologicalSpace X] [RegularSpace X]
  (h : ∀ x A, IsClosed A ∧ ¬ x ∈ A →
  ∃ (f : X → I), Continuous f ∧ f x = (1 : I) ∧ f '' A = {0}) :
  IsConnected (univ : Set X) ↔ IsConnected (univ : Set (StoneCech X)) :=

X✝ : Type u_1
X : Type u_2
inst✝¹ : TopologicalSpace X
inst✝ : RegularSpace X
h : ∀ (x : X) (A : Set X), IsClosed A ∧ x ∉ A → ∃ f, Continuous f ∧ f x = 1 ∧ f '' A = {0}
⊢ IsConnected univ ↔ IsConnected univ


/-- Let $(X, d_X)$ and $(Y, d_Y)$ be metric spaces; let $Y$ be complete. Let $A \subset X$. Show that if $f \colon A \rightarrow Y$ is uniformly continuous, then $f$ can be uniquely extended to a continuous function $g \colon \bar{A} \rightarrow Y$, and $g$ is uniformly continuous.-/

theorem exercise_43_2 {X : Type*} [MetricSpace X]
  {Y : Type*} [MetricSpace Y] [CompleteSpace Y] (A : Set X)
  (f : X → Y) (hf : UniformContinuousOn f A) :
  ∃! (g : X → Y), ContinuousOn g (closure A) ∧
  UniformContinuousOn g (closure A) ∧ ∀ (x : A), g x = f x :=

X : Type u_1
inst✝² : MetricSpace X
Y : Type u_2
inst✝¹ : MetricSpace Y
inst✝ : CompleteSpace Y
A : Set X
f : X → Y
hf : UniformContinuousOn f A
⊢ ∃! g, ContinuousOn g (closure A) ∧ UniformContinuousOn g (closure A) ∧ ∀ (x : ↑A), g ↑x = f ↑x


/-- For all odd $n$ show that $8 \mid n^{2}-1$.-/

theorem exercise_1_27 {n : ℕ} (hn : Odd n) : 8 ∣ (n^2 - 1) :=

import Mathlib

open Real
open scoped BigOperators


/-- Prove that $\frac{1}{2}+\frac{1}{3}+\cdots+\frac{1}{n}$ is not an integer.-/

theorem exercise_1_30 {n : ℕ} :
  ¬ ∃ a : ℤ, ∑ i : Fin n, (1 : ℚ) / (n+2) = a :=

n : ℕ
⊢ ¬∃ a, ∑ i : Fin n, 1 / (↑n + 2) = ↑a


/-- Show that 2 is divisible by $(1+i)^{2}$ in $\mathbb{Z}[i]$.-/

theorem exercise_1_31  : (⟨1, 1⟩ : GaussianInt) ^ 2 ∣ 2 :=


/-- If $a$ is a nonzero integer, then for $n>m$ show that $\left(a^{2^{n}}+1, a^{2^{m}}+1\right)=1$ or 2 depending on whether $a$ is odd or even.-/

theorem exercise_2_4 {a : ℤ} (ha : a ≠ 0)
  (f_a := λ n m : ℕ => Int.gcd (a^(2^n) + 1) (a^(2^m)+1)) {n m : ℕ}
  (hnm : n > m) :
  (Odd a → f_a n m = 1) ∧ (Even a → f_a n m = 2) :=

a : ℤ
ha : a ≠ 0
f_a : optParam (ℕ → ℕ → ℕ) fun n m => (a ^ 2 ^ n + 1).gcd (a ^ 2 ^ m + 1)
n m : ℕ
hnm : n > m
⊢ (Odd a → f_a n m = 1) ∧ (Even a → f_a n m = 2)


/-- Define $\wedge(n)=\log p$ if $n$ is a power of $p$ and zero otherwise. Prove that $\sum_{A \mid n} \mu(n / d) \log d$ $=\wedge(n)$.-/

theorem exercise_2_21 {l : ℕ → ℝ}
  (hl : ∀ p n : ℕ, p.Prime → l (p^n) = log p )
  (hl1 : ∀ m : ℕ, ¬ IsPrimePow m → l m = 0) :
  l = λ n => ∑ d : Nat.divisors n, ArithmeticFunction.moebius (n/d) * log d :=

l : ℕ → ℝ
hl : ∀ (p n : ℕ), p.Prime → l (p ^ n) = (↑p).log
hl1 : ∀ (m : ℕ), ¬IsPrimePow m → l m = 0
⊢ l = fun n => ∑ d : { x // x ∈ n.divisors }, ↑(ArithmeticFunction.moebius (n / ↑d)) * (↑↑d).log


/-- Show that $\sum^{\prime} 1 / n$, the sum being over square free integers, diverges.-/

theorem exercise_2_27a :
  ¬ Summable (λ i : {p : ℤ // Squarefree p} => (1 : ℚ) / i) :=


/-- Show that there are infinitely many primes congruent to $-1$ modulo 6 .-/

theorem exercise_3_1 : Infinite {p : Nat.Primes // p ≡ -1 [ZMOD 6]} :=


/-- Show that the equation $3 x^{2}+2=y^{2}$ has no solution in integers.-/

theorem exercise_3_4 : ¬ ∃ x y : ℤ, 3*x^2 + 2 = y^2 :=


/-- Show that the equation $7 x^{3}+2=y^{3}$ has no solution in integers.-/

theorem exercise_3_5 : ¬ ∃ x y : ℤ, 7*x^3 + 2 = y^3 :=


/-- If $n$ is not a prime, show that $(n-1) ! \equiv 0(n)$, except when $n=4$.-/

theorem exercise_3_10 {n : ℕ} (hn0 : ¬ n.Prime) (hn1 : n ≠ 4) :
  Nat.factorial (n-1) ≡ 0 [MOD n] :=

n : ℕ
hn0 : ¬n.Prime
hn1 : n ≠ 4
⊢ (n - 1).factorial ≡ 0 [MOD n]


/-- Let $p$ and $q$ be distinct odd primes such that $p-1$ divides $q-1$. If $(n, p q)=1$, show that $n^{q-1} \equiv 1(p q)$.-/

theorem exercise_3_14 {p q n : ℕ} (hp0 : p.Prime ∧ p > 2)
  (hq0 : q.Prime ∧ q > 2) (hpq0 : p ≠ q) (hpq1 : p - 1 ∣ q - 1)
  (hn : n.gcd (p*q) = 1) :
  n^(q-1) ≡ 1 [MOD p*q] :=

p q n : ℕ
hp0 : p.Prime ∧ p > 2
hq0 : q.Prime ∧ q > 2
hpq0 : p ≠ q
hpq1 : p - 1 ∣ q - 1
hn : n.gcd (p * q) = 1
⊢ n ^ (q - 1) ≡ 1 [MOD p * q]


/-- Consider a prime $p$ of the form $4 t+1$. Show that $a$ is a primitive root modulo $p$ iff $-a$ is a primitive root modulo $p$.-/

theorem exercise_4_4 {p t: ℕ} (hp0 : p.Prime) (hp1 : p = 4*t + 1)
  (a : ZMod p) :
  IsPrimitiveRoot a p ↔ IsPrimitiveRoot (-a) p :=

p t : ℕ
hp0 : p.Prime
hp1 : p = 4 * t + 1
a : ZMod p
⊢ IsPrimitiveRoot a p ↔ IsPrimitiveRoot (-a) p


/-- Consider a prime $p$ of the form $4 t+3$. Show that $a$ is a primitive root modulo $p$ iff $-a$ has order $(p-1) / 2$.-/

theorem exercise_4_5 {p t : ℕ} (hp0 : p.Prime) (hp1 : p = 4*t + 3)
  (a : ZMod p) :
  IsPrimitiveRoot a p ↔ ((-a) ^ ((p-1)/2) = 1 ∧ ∀ (k : ℕ), k < (p-1)/2 → (-a)^k ≠ 1) :=

p t : ℕ
hp0 : p.Prime
hp1 : p = 4 * t + 3
a : ZMod p
⊢ IsPrimitiveRoot a p ↔ (-a) ^ ((p - 1) / 2) = 1 ∧ ∀ k < (p - 1) / 2, (-a) ^ k ≠ 1


/-- If $p=2^{n}+1$ is a Fermat prime, show that 3 is a primitive root modulo $p$.-/

theorem exercise_4_6 {p n : ℕ} (hp : p.Prime) (hpn : p = 2^n + 1) :
  IsPrimitiveRoot 3 p :=

p n : ℕ
hp : p.Prime
hpn : p = 2 ^ n + 1
⊢ IsPrimitiveRoot 3 p


/-- Let $p$ be an odd prime. Show that $a$ is a primitive root modulo $p$ iff $a^{(p-1) / q} \not \equiv 1(p)$ for all prime divisors $q$ of $p-1$.-/

theorem exercise_4_8 {p a : ℕ} (hp : Odd p) :
  IsPrimitiveRoot a p ↔ (∀ q : ℕ, q ∣ (p-1) → q.Prime → ¬ a^(p-1) ≡ 1 [MOD p]) :=

p a : ℕ
hp : Odd p
⊢ IsPrimitiveRoot a p ↔ ∀ (q : ℕ), q ∣ p - 1 → q.Prime → ¬a ^ (p - 1) ≡ 1 [MOD p]


theorem exercise_4_11 {p : ℕ} (hp : p.Prime) (k s: ℕ)
  (s := ∑ n : Fin p, (n : ℕ) ^ k) :
  ((¬ p - 1 ∣ k) → s ≡ 0 [MOD p]) ∧ (p - 1 ∣ k → s ≡ 0 [MOD p]) :=

p : ℕ
hp : p.Prime
k s✝ : ℕ
s : optParam ℕ (∑ n : Fin p, ↑n ^ k)
⊢ (¬p - 1 ∣ k → s ≡ 0 [MOD p]) ∧ (p - 1 ∣ k → s ≡ 0 [MOD p])


/-- Show that any prime divisor of $x^{4}-x^{2}+1$ is congruent to 1 modulo 12 .-/

theorem exercise_5_13 {p x: ℤ} (hp : Prime p)
  (hpx : p ∣ (x^4 - x^2 + 1)) : p ≡ 1 [ZMOD 12] :=

p x : ℤ
hp : Prime p
hpx : p ∣ x ^ 4 - x ^ 2 + 1
⊢ p ≡ 1 [ZMOD 12]


/-- Show that $x^{4} \equiv 2(p)$ has a solution for $p \equiv 1(4)$ iff $p$ is of the form $A^{2}+64 B^{2}$.-/

theorem exercise_5_28 {p : ℕ} (hp : p.Prime) (hp1 : p ≡ 1 [MOD 4]):
  ∃ x, x^4 ≡ 2 [MOD p] ↔ ∃ A B, p = A^2 + 64*B^2 :=

p : ℕ
hp : p.Prime
hp1 : p ≡ 1 [MOD 4]
⊢ ∃ x, x ^ 4 ≡ 2 [MOD p] ↔ ∃ A B, p = A ^ 2 + 64 * B ^ 2


/-- Show that if $a$ is negative then $p \equiv q(4 a) together with p\not | a$ imply $(a / p)=(a / q)$.-/

theorem exercise_5_37 {p q : ℕ} [Fact (p.Prime)] [Fact (q.Prime)] {a : ℤ}
  (ha : a < 0) (h0 : p ≡ q [ZMOD 4*a]) (h1 : ¬ ((p : ℤ) ∣ a)) :
  legendreSym p a = legendreSym q a :=

p q : ℕ
inst✝¹ : Fact p.Prime
inst✝ : Fact q.Prime
a : ℤ
ha : a < 0
h0 : ↑p ≡ ↑q [ZMOD 4 * a]
h1 : ¬↑p ∣ a
⊢ legendreSym p a = legendreSym q a


/-- Show that $\sin (\pi / 12)$ is an algebraic number.-/

theorem exercise_12_12 : IsAlgebraic ℚ (sin (Real.pi/12)) :=


/-- Show that 1729 is the smallest positive integer expressible as the sum of two different integral cubes in two ways.-/

theorem exercise_18_4 {n : ℕ} (hn : ∃ x y z w : ℤ,
  x^3 + y^3 = n ∧ z^3 + w^3 = n ∧ x ≠ z ∧ x ≠ w ∧ y ≠ z ∧ y ≠ w) :
  n ≥ 1729 :=

n : ℕ
hn : ∃ x y z w, x ^ 3 + y ^ 3 = ↑n ∧ z ^ 3 + w ^ 3 = ↑n ∧ x ≠ z ∧ x ≠ w ∧ y ≠ z ∧ y ≠ w
⊢ n ≥ 1729


z : Fin 4 → ℂ
hz0 : ∀ (n : Fin 4), ‖z n‖ < 1
hz1 : ∀ (n : Fin 4), z n ≠ 1
⊢ 3 - z 0 - z 1 - z 2 - z 3 + z 0 * z 1 * z 2 * z 3 ≠ 0

import Mathlib

open scoped BigOperators

open scoped BigOperators


/-- Let $f: \mathbb{R} \rightarrow \mathbb{R}$ be an infinitely differentiable function satisfying $f(0)=0, f(1)=1$, and $f(x) \geq 0$ for all $x \in$ $\mathbb{R}$. Show that there exist a positive integer $n$ and a real number $x$ such that $f^{(n)}(x)<0$.-/

theorem exercise_2018_a5 (f : ℝ → ℝ) (hf : ContDiff ℝ ⊤ f)
  (hf0 : f 0 = 0) (hf1 : f 1 = 1) (hf2 : ∀ x, f x ≥ 0) :
  ∃ (n : ℕ) (x : ℝ), iteratedDeriv n f x = 0 :=

f : ℝ → ℝ
hf : ContDiff ℝ ⊤ f
hf0 : f 0 = 0
hf1 : f 1 = 1
hf2 : ∀ (x : ℝ), f x ≥ 0
⊢ ∃ n x, iteratedDeriv n f x = 0

import Mathlib

open scoped BigOperators


/-- Let $n$ be a positive integer, and let $f_{n}(z)=n+(n-1) z+$ $(n-2) z^{2}+\cdots+z^{n-1}$. Prove that $f_{n}$ has no roots in the closed unit disk $\{z \in \mathbb{C}:|z| \leq 1\}$.-/

theorem exercise_2018_b2 (n : ℕ) (hn : n > 0) (f : ℕ → ℂ → ℂ)
  (hf : ∀ n : ℕ, f n = λ (z : ℂ) => (∑ i : Fin n, (n-i)* z^(i : ℕ))) :
  ¬ (∃ z : ℂ, ‖z‖ ≤ 1 ∧ f n z = 0) :=

n : ℕ
hn : n > 0
f : ℕ → ℂ → ℂ
hf : ∀ (n : ℕ), f n = fun z => ∑ i : Fin n, (↑n - ↑↑i) * z ^ ↑i
⊢ ¬∃ z, ‖z‖ ≤ 1 ∧ f n z = 0


/-- Given a real number $a$, we define a sequence by $x_{0}=1$, $x_{1}=x_{2}=a$, and $x_{n+1}=2 x_{n} x_{n-1}-x_{n-2}$ for $n \geq 2$. Prove that if $x_{n}=0$ for some $n$, then the sequence is periodic.-/

theorem exercise_2018_b4 (a : ℝ) (x : ℕ → ℝ) (hx0 : x 0 = a)
  (hx1 : x 1 = a)
  (hxn : ∀ n : ℕ, n ≥ 2 → x (n+1) = 2*(x n)*(x (n-1)) - x (n-2))
  (h : ∃ n, x n = 0) :
  ∃ c, Function.Periodic x c :=

a : ℝ
x : ℕ → ℝ
hx0 : x 0 = a
hx1 : x 1 = a
hxn : ∀ n ≥ 2, x (n + 1) = 2 * x n * x (n - 1) - x (n - 2)
h : ∃ n, x n = 0
⊢ ∃ c, Function.Periodic x c


/-- Suppose that $f(x)=\sum_{i=0}^{\infty} c_{i} x^{i}$ is a power series for which each coefficient $c_{i}$ is 0 or 1 . Show that if $f(2 / 3)=3 / 2$, then $f(1 / 2)$ must be irrational.-/

f : ℝ → ℝ
c : ℕ → ℝ
hf : f = fun x => ∑' (i : ℕ), c i * x ^ i
hc : ∀ (n : ℕ), c n = 0 ∨ c n = 1
hf1 : f (2 / 3) = 3 / 2
⊢ Irrational (f (1 / 2))


theorem exercise_2014_a5 (P : ℕ → Polynomial ℤ)
  (hP : ∀ n, P n = ∑ i : Fin n, (n+1) * Polynomial.X ^ n) :
  ∀ (j k : ℕ), j ≠ k → IsCoprime (P j) (P k) :=

P : ℕ → Polynomial ℤ
hP : ∀ (n : ℕ), P n = ∑ i : Fin n, (↑n + 1) * Polynomial.X ^ n
⊢ ∀ (j k : ℕ), j ≠ k → IsCoprime (P j) (P k)


/-- Prove that for each positive integer $n$, the number $10^{10^{10^n}}+10^{10^n}+10^n-1$ is not prime.-/


/-- Prove that there are unique positive integers $a, n$ such that $a^{n+1}-(a+1)^n=2001$.-/


/-- Prove that there exist infinitely many integers $n$ such that $n, n+1, n+2$ are each the sum of the squares of two integers.-/


/-- Let $f$ be a real function with a continuous third derivative such that $f(x), f^{\prime}(x), f^{\prime \prime}(x), f^{\prime \prime \prime}(x)$ are positive for all $x$. Suppose that $f^{\prime \prime \prime}(x) \leq f(x)$ for all $x$. Show that $f^{\prime}(x)<2 f(x)$ for all $x$.-/

theorem exercise_1999_b4 (f : ℝ → ℝ) (hf: ContDiff ℝ 3 f)
  (hf1 : ∀ n ≤ 3, ∀ x : ℝ, iteratedDeriv n f x > 0)
  (hf2 : ∀ x : ℝ, iteratedDeriv 3 f x ≤ f x) :
  ∀ x : ℝ, deriv f x < 2 * f x :=

f : ℝ → ℝ
hf : ContDiff ℝ 3 f
hf1 : ∀ n ≤ 3, ∀ (x : ℝ), iteratedDeriv n f x > 0
hf2 : ∀ (x : ℝ), iteratedDeriv 3 f x ≤ f x
⊢ ∀ (x : ℝ), deriv f x < 2 * f x


/-- Let $f$ be a real function on the real line with continuous third derivative. Prove that there exists a point $a$ such that-/

theorem exercise_1998_a3 (f : ℝ → ℝ) (hf : ContDiff ℝ 3 f) :
  ∃ a : ℝ, (f a) * (deriv f a) * (iteratedDeriv 2 f a) * (iteratedDeriv 3 f a) ≥ 0 :=

f : ℝ → ℝ
hf : ContDiff ℝ 3 f
⊢ ∃ a, f a * deriv f a * iteratedDeriv 2 f a * iteratedDeriv 3 f a ≥ 0


/-- Prove that, for any integers $a, b, c$, there exists a positive integer $n$ such that $\sqrt{n^3+a n^2+b n+c}$ is not an integer.-/

theorem exercise_1998_b6 (a b c : ℤ) :
  ∃ n : ℤ, n > 0 ∧ ¬ ∃ m : ℤ, Real.sqrt (n^3 + a*n^2 + b*n + c) = m :=

a b c : ℤ
⊢ ∃ n > 0, ¬∃ m, √(↑n ^ 3 + ↑a * ↑n ^ 2 + ↑b * ↑n + ↑c) = ↑m