Library Phi: Hofstadter G function and the golden ratio


Require Import Arith Fourier R_Ifp R_sqrt Znumtheory.
Require Import Fib FunG.

Open Scope R.

We consider again the function g defined by g 0 = 0 and g (S n) = S n - g (g n), and we prove that it can also be directly defined via primitives using reals numbers:
g(n) = floor((n+1)/phi) = floor(tau*(n+1))
where:
In Coq, we'll use here by default operations on reals numbers, and also:
  • INR : the injection from nat to R
  • IZR : the injection from Z to R
  • Int_part : the integer part of a real, used as a floor function. It produces a Z integer, that we can convert to nat via Z.to_nat when necessary.
  • frac_part : the faction part of a real, producing a R

Phi and tau


Definition phi := (sqrt 5 + 1)/2.
Definition tau := (sqrt 5 - 1)/2.

Lemma tau_1 : tau + 1 = phi.

Lemma tau_phi : tau × phi = 1.

Lemma tau_tau : tau × tau = 1 - tau.

Lemma tau_nz : tau 0.

Lemma phi_nz : phi 0.

Lemma tau_inv : tau = 1/phi.

Lemma tau_bound : 6/10 < tau < 7/10.

A bit of irrationality theory


Lemma prime_5 : prime 5.

Lemma prime_irr (r:R) :
 ( (p q:Z), Z.gcd p q = 1%Zr × IZR q IZR p) →
 ( (p q:Z), r × IZR q = IZR pq=0%Z).

Lemma sqrt5_irr (p q:Z) : sqrt 5 × IZR q = IZR pq = 0%Z.

Lemma tau_irr (p q:Z) : tau × IZR q = IZR pq = 0%Z.

Some complements about integer part and fractional part


Lemma int_part_iff (r:R)(k:Z) :
 0 r-IZR k < 1 Int_part r = k.

Lemma int_part_carac (r:R)(k:Z) :
 0 r-IZR k < 1 → Int_part r = k.

Lemma int_frac r : r = IZR (Int_part r) + frac_part r.

Lemma int_part_le (r:R)(k:Z) : IZR k r (k Int_part r)%Z.

The main theorem


Lemma g_tau (n:nat) : g n = Z.to_nat (Int_part (tau × INR (S n))).
Lemma g_phi (n:nat) : g n = Z.to_nat (Int_part (INR (S n)/phi)).