2015-07-10 01:56:48 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
-/
|
|
|
|
import data.nat
|
2015-10-14 19:27:09 +00:00
|
|
|
open nat algebra
|
2015-07-10 01:56:48 +00:00
|
|
|
|
|
|
|
definition partial_sum : nat → nat
|
|
|
|
| 0 := 0
|
|
|
|
| (succ n) := succ n + partial_sum n
|
|
|
|
|
|
|
|
example : partial_sum 5 = 15 :=
|
|
|
|
rfl
|
|
|
|
|
|
|
|
example : partial_sum 6 = 21 :=
|
|
|
|
rfl
|
|
|
|
|
|
|
|
lemma two_mul_partial_sum_eq : ∀ n, 2 * partial_sum n = (succ n) * n
|
|
|
|
| 0 := by reflexivity
|
|
|
|
| (succ n) := calc
|
2015-10-14 19:27:09 +00:00
|
|
|
2 * (succ n + partial_sum n) = 2 * succ n + 2 * partial_sum n : left_distrib
|
2015-07-10 01:56:48 +00:00
|
|
|
... = 2 * succ n + succ n * n : two_mul_partial_sum_eq
|
|
|
|
... = 2 * succ n + n * succ n : mul.comm
|
2015-10-14 19:27:09 +00:00
|
|
|
... = (2 + n) * succ n : right_distrib
|
2015-07-10 01:56:48 +00:00
|
|
|
... = (n + 2) * succ n : add.comm
|
|
|
|
... = (succ (succ n)) * succ n : rfl
|
|
|
|
|
|
|
|
theorem partial_sum_eq : ∀ n, partial_sum n = ((n + 1) * n) div 2 :=
|
|
|
|
take n,
|
|
|
|
assert h₁ : (2 * partial_sum n) div 2 = ((succ n) * n) div 2, by rewrite two_mul_partial_sum_eq,
|
2015-10-12 03:29:31 +00:00
|
|
|
assert h₂ : (2:nat) > 0, from dec_trivial,
|
2015-07-10 01:56:48 +00:00
|
|
|
by rewrite [mul_div_cancel_left _ h₂ at h₁]; exact h₁
|