Require Import Arith Lia Ring.  

(* composite number just means it can be written as ab where both a and b > 1 *)
Definition composite (n: nat) : Prop :=  
  exists x y, 1 < x /\ 1 < y /\ n = x * y.  

Lemma Putnam_1988_B1 n :  
  3 < n ->  
  composite n ->  
  exists x y z : nat,  
     0 < x /\ 0 < y /\ 0 < z  
  /\ n = x*y + y*z + z*x + 1.  
Proof.  

  (* given that n > 3 and is composite, so we can find a and b such that n = a * b, and a,b > 1 *)
  intros _ [a [b [Ha [Hb Hab]]]].  

  (* now the goal is to write n = xy + yz + zx + 1 for some x y z > 0 *)

  (* maybe we can go backwards? like if n = (x+1)(y+1) = xy + x + y + 1 *)
  (* then x = a - 1, y = b - 1 should work since a = x + 1, b = y + 1 *)

  set (x := a - 1).  (* x is a bit less than a, so a = x + 1 *)
  set (y := b - 1).  (* same for y, b = y + 1 *)

  (* trying z = 1 cause then the thing might act like a rotated form or reshaped to (x+1)(y+1), which is ab *)
  set (z := 1).      

  (* now we show x, y, z are positive, since the problem wants positive integers *)
  exists x, y, z.  

  (* since a > 1, then x = a - 1 > 0 *)
  split; [ lia | ].

  (* same thing, b > 1 ⇒ y = b - 1 > 0 *)
  split; [ lia | ].

  (* z is 1 so obviously positive *)
  split; [ lia | ].

  (* next step is to prove that n = xy + yz + zx + 1 *)
  (* hopefully this works since we know n = a*b and a = x+1, b = y+1 *)

  unfold x, y, z.   (* need to see what the real expressions are *)

  (* replace n = ab using what we were given *)
  rewrite Hab.  

  (* just expanding and simplifying now *)
  simpl.

  (* should just be some linear terms, so lia can take care of it *)
  lia.  
Qed.
