(* ========================================================================= *)
(* Sum of reciprocals of triangular numbers.                                 *)
(* ========================================================================= *)

needs "Multivariate/misc.ml";;  (*** Just for REAL_ARCH_INV! ***)

prioritize_real();;

(* ------------------------------------------------------------------------- *)
(* Definition of triangular numbers.                                         *)
(* ------------------------------------------------------------------------- *)

let triangle = new_definition
 `triangle n = (n * (n + 1)) DIV 2`;;

(* ------------------------------------------------------------------------- *)
(* Mapping them into the reals: division is exact.                           *)
(* ------------------------------------------------------------------------- *)

let REAL_TRIANGLE = `&(triangle n) = (&n * (&n + &1)) / &2`;;

(* ------------------------------------------------------------------------- *)
(* Sum of a finite number of terms.                                          *)
(* ------------------------------------------------------------------------- *)

let TRIANGLE_FINITE_SUM = `!n. sum(1..n) (\k. &1 / &(triangle k)) = &2 - &2 / (&n + &1)`;;

(* ------------------------------------------------------------------------- *)
(* Hence limit.                                                              *)
(* ------------------------------------------------------------------------- *)

let TRIANGLE_CONVERGES = `!e. &0 < e
       ==> ?N. !n. n >= N
                   ==> abs(sum(1..n) (\k. &1 / &(triangle k)) - &2) < e`;;

(* ------------------------------------------------------------------------- *)
(* In terms of limits.                                                       *)
(* ------------------------------------------------------------------------- *)

needs "Library/analysis.ml";;

override_interface ("-->",`(tends_num_real)`);;

let TRIANGLE_CONVERGES' = `(\n. sum(1..n) (\k. &1 / &(triangle k))) --> &2`;;