import Mathlib.FieldTheory.IsAlgClosed.AlgebraicClosureimport Mathlib.Algebra.CharP.Lemmasimport Mathlib.Algebra.Field.ZModimport Mathlib.Data.Nat.Fib.Basicimport Mathlib.Data.Nat.Prime.Infiniteimport Mathlib.Tactic.Ringnamespace FibSpacingopen Polynomialopen scoped BigOperatorsdef IntegralMean (n : ℕ) : Prop := 2 ≤ n ∧ n ∣ ∑ i ∈ Finset.range (n+1), Nat.fib iprivate lemma fib_one {K : Type*} [Field K] {p n : ℕ} [CharP K p](hn : IntegralMean n) (hp : p ∣ n) : (Nat.fib (n+2) : K) = 1 := byobtain ⟨k,hk⟩ := hp.trans hn.2rw [Nat.fib_succ_eq_succ_sum (n+1), hk]simpprivate lemma binet {K : Type*} [Field K] {r s : K}(hr : r^2 = r+1) (hs : s^2 = s+1) (n : ℕ) :(Nat.fib n : K) * (r-s) = r^n - s^n := byinduction n using Nat.twoStepInduction with| zero => simp| one => simp| more n ha hb =>rw [Nat.fib_add_two, Nat.cast_add, add_mul, ha, hb]simp only [pow_succ]linear_combination -r^n*hr + s^n*hsprivate lemma three_values {K : Type*} [Field K] {A B R S : K}(a : A ≠ 0) (b : B ≠ 0) (ne : A-B ≠ 0)(h : A*R-B*S = A-B) (j : A*R^2-B*S^2 = A-B) : R=1 ∧ S=1 := byhave z : A*(R-1)*(R-S) = 0 := bylinear_combination j - (S+1)*hhave e : R=S := byrcases mul_eq_zero.mp z with t|t· have hr := sub_eq_zero.mp ((mul_eq_zero.mp t).resolve_left a)rw [hr] at hhave hb : B*(S-1)=0 := by linear_combination -hexact hr.trans (sub_eq_zero.mp ((mul_eq_zero.mp hb).resolve_left b)).symm· exact sub_eq_zero.mp tsubst Shave eq : (A-B)*(R-1)=0 := by linear_combination hexact ⟨sub_eq_zero.mp ((mul_eq_zero.mp eq).resolve_left ne), byexact sub_eq_zero.mp ((mul_eq_zero.mp eq).resolve_left ne)⟩private lemma roots {K : Type*} [Field K] [IsAlgClosed K] (five : (5:K) ≠ 0) :∃ r s : K, r^2 = r+1 ∧ s^2 = s+1 ∧ r ≠ s ∧ r ≠ 0 ∧ s ≠ 0 := bylet f : K[X] := X^2 - (X+1)have deg : f.degree ≠ 0 := bydsimp [f]rw [degree_sub_eq_left_of_degree_lt]· simp· rw [← C_1, degree_X_add_C, degree_X_pow]; decideobtain ⟨r,h⟩ := IsAlgClosed.exists_root f deghave hr : r^2 = r+1 := sub_eq_zero.mp (by simpa [IsRoot,f] using h)refine ⟨r,1-r,hr,?_,?_,?_,?_⟩· linear_combination hr· intro eapply fivelinear_combination (2*r-1)*e - 4*hr· rintro rflsimp at hr· intro ehave := sub_eq_zero.mp erw [← this] at hrsimp at hrtheorem local_period (a d p : ℕ) (prime : p.Prime) (unramified : p ≠ 5) (unit : ¬p ∣ d)(H : ∀ j < 3*p, IntegralMean (a+j*d)) :p ∣ Nat.fib d ∧ (Nat.fib (d+1) : ZMod p) = 1 := bylet : Fact p.Prime := ⟨prime⟩let K := AlgebraicClosure (ZMod p)have five : (5:K) ≠ 0 := bychange ((5:ℕ):K) ≠ 0intro hexact unramified ((Nat.prime_dvd_prime_iff_eq prime (by decide)).mp ((CharP.cast_eq_zero_iff K p 5).mp h))obtain ⟨r,s,hr,hs,ne,r0,s0⟩ := roots fivelet x : ZMod p := -(a:ZMod p)/(d:ZMod p)let i := x.valhave hi : i < p := x.val_lthave h0 : p ∣ a+i*d := byapply (CharP.cast_eq_zero_iff (ZMod p) p _).mphave hd := mt (CharP.cast_eq_zero_iff (ZMod p) p d).mp unitsimp [i, x, div_mul_cancel₀ _ hd]let n := a+i*d+2let q := d*phave hit (t : ℕ) (ht : t ≤ 2) : (Nat.fib (n+t*q) : K) = 1 := byhave hidx : i+t*p < 3*p := byhave := Nat.mul_le_mul_right p htomegahave hdiv : p ∣ a+(i+t*p)*d := byrw [Nat.add_mul, ← Nat.add_assoc]exact dvd_add h0 (dvd_mul_of_dvd_left (dvd_mul_left p t) d)rw [show n+t*q = a+(i+t*p)*d+2 by dsimp [n,q]; ring]exact fib_one (K := K) (H _ hidx) hdivhave hb (t : ℕ) (ht : t ≤ 2) :r^n*(r^q)^t - s^n*(s^q)^t = r-s := byrw [← pow_mul, ← pow_mul, mul_comm q t, ← pow_add, ← pow_add,← binet hr hs, hit t ht, one_mul]have hbase : r^n-s^n = r-s := by simpa using hb 0 (by decide)have eqs := three_values (pow_ne_zero n r0) (pow_ne_zero n s0)(hbase ▸ sub_ne_zero.mpr ne) (by simpa [hbase] using hb 1 (by decide))(by simpa [hbase] using hb 2 (by decide))have descend (y : K) (h : y^q=1) : y^d=1 := byapply sub_eq_zero.mpapply (pow_eq_zero_iff (n := p) prime.ne_zero).mprw [sub_pow_char, one_pow, ← pow_mul, h, sub_self]obtain ⟨u,v⟩ := eqshave hR := descend r uhave hS := descend s vhave hd := binet hr hs drw [hR,hS,sub_self] at hdrefine ⟨(CharP.cast_eq_zero_iff K p _).mp ((mul_eq_zero.mp hd).resolve_right (sub_ne_zero.mpr ne)), ?_⟩have hh := binet hr hs (d+1)rw [pow_succ,pow_succ,hR,hS,one_mul,one_mul] at hhhave hone : (Nat.fib (d+1) : K) = 1 :=mul_right_cancel₀ (sub_ne_zero.mpr ne) (hh.trans (one_mul _).symm)apply (algebraMap (ZMod p) K).injectivesimpa using honetheorem fixed_difference_bound (d : ℕ) (hd : 0 < d) :∃ k : ℕ, ∀ a : ℕ, ¬ ∀ j < k, IntegralMean (a+j*d) := byobtain ⟨p,large,hp⟩ := Nat.exists_infinite_primes (6+d+Nat.fib d)refine ⟨3*p, fun a H => ?_⟩have no : ¬p ∣ d := fun h => by have := Nat.le_of_dvd hd h; omegahave h := (local_period a d p hp (by omega) no H).1have := Nat.le_of_dvd (Nat.fib_pos.mpr hd) homegaend FibSpacing