Mathlib.Data.Nat.Factorial.Basic
84 declarations. Mathlib docs · Source
Factorial and variants
This file defines the factorial, along with the ascending and descending variants.
For the proof that the factorial of n counts the permutations of an n-element set,
see Fintype.card_perm.
Main declarations
Nat.factorial: The factorial.Nat.ascFactorial: The ascending factorial. It is the product of natural numbers fromnton + k - 1.Nat.descFactorial: The descending factorial. It is the product of natural numbers fromn - k + 1ton.
Ascending and descending factorials
Factorial via binary splitting.
We prove this is equal to the standard factorial and mark it @[csimp].
We could proceed further, with either Legendre or Luschny methods.
This is the highest factorial I can #eval using the naive implementation without a stack overflow:
/-- info: 114716 -/
#guard_msgs in
#eval 9718 ! |>.log2
Similarly, evaluation of ascFactorial 100 15000 fails with the naive implementation
but works with the binary recursion.
We could implement a tail-recursive version (or just use Nat.fold),
but instead let's jump straight to binary splitting.
We are now limited by time, not stack space, and this is much faster than even the tail-recursive version.
#time -- Less than 1s. (Tail-recursive version takes longer for `(10^5) !`.)
#eval (10^6) ! |>.log2
theorem Nat.factorial.match_1.congr_eq_1 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ),
x = 0 →
(match x with
| 0 => h_1 ()
| n.succ => h_2 n) ≍
h_1 ()
theorem Nat.ascFactorialBinary.induct_unfolding E1 #
∀ (motive : ℕ → ℕ → ℕ → Prop),
(∀ (n : ℕ), motive n 0 1) →
(∀ (n : ℕ), motive n 1 n) →
(∀ (n n_1 : ℕ),
motive n ((n_1 + 2) / 2) (n.ascFactorialBinary ((n_1 + 2) / 2)) →
motive (n + (n_1 + 2) / 2) ((n_1 + 2 + 1) / 2)
((n + (n_1 + 2) / 2).ascFactorialBinary ((n_1 + 2 + 1) / 2)) →
motive n n_1.succ.succ
(n.ascFactorialBinary ((n_1 + 2) / 2) * (n + (n_1 + 2) / 2).ascFactorialBinary ((n_1 + 2 + 1) / 2))) →
∀ (n k : ℕ), motive n k (n.ascFactorialBinary k)
theorem Nat.factorial.match_1.congr_eq_2 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ) (n : ℕ),
x = n.succ →
(match x with
| 0 => h_1 ()
| n.succ => h_2 n) ≍
h_2 n
theorem Nat.ascFactorialBinary.match_1.congr_eq_2 E1 #
∀ (motive : ℕ → Sort u_1) (k : ℕ) (h_1 : Unit → motive 0) (h_2 : Unit → motive 1)
(h_3 : (k n : ℕ) → k = n + 2 → motive n.succ.succ),
k = 1 →
(match k with
| 0 => h_1 ()
| 1 => h_2 ()
| k@h:n.succ.succ => h_3 k n h) ≍
h_2 ()
theorem Nat.ascFactorialBinary.match_1.congr_eq_1 E1 #
∀ (motive : ℕ → Sort u_1) (k : ℕ) (h_1 : Unit → motive 0) (h_2 : Unit → motive 1)
(h_3 : (k n : ℕ) → k = n + 2 → motive n.succ.succ),
k = 0 →
(match k with
| 0 => h_1 ()
| 1 => h_2 ()
| k@h:n.succ.succ => h_3 k n h) ≍
h_1 ()
theorem Nat.ascFactorialBinary.match_1.congr_eq_3 E1 #
∀ (motive : ℕ → Sort u_1) (k : ℕ) (h_1 : Unit → motive 0) (h_2 : Unit → motive 1)
(h_3 : (k n : ℕ) → k = n + 2 → motive n.succ.succ) (n : ℕ),
k = n.succ.succ →
(match k with
| 0 => h_1 ()
| 1 => h_2 ()
| k@h:n.succ.succ => h_3 k n h) ≍
h_3 (n + 2) n ⋯
def Nat.factorial E3 #
ℕ → ℕ
Nat.factorial n is the factorial of n.
def Nat.term_! E1 #
Lean.TrailingParserDescr
factorial notation (n)! for Nat.factorial n.
In Lean, names can end with exclamation marks (e.g. List.get!), so you cannot write
n! in Lean, but must write (n)! or n ! instead. The former is preferred, since
Lean can confuse the ! in n ! as the (prefix) Boolean negation operation in some
cases.
For numerals the parentheses are not required, so e.g. 0! or 1! work fine.
theorem Nat.factorial_zero E0 #
Nat.factorial 0 = 1
theorem Nat.factorial_succ E1 #
∀ (n : ℕ), (n + 1).factorial = (n + 1) * n.factorial
theorem Nat.factorial_one E0 #
Nat.factorial 1 = 1
theorem Nat.factorial_two E0 #
Nat.factorial 2 = 2
theorem Nat.mul_factorial_pred E1 #
∀ {n : ℕ}, n ≠ 0 → n * (n - 1).factorial = n.factorial
theorem Nat.factorial_pos E1 #
∀ (n : ℕ), 0 < n.factorial
theorem Nat.factorial_ne_zero E1 #
∀ (n : ℕ), n.factorial ≠ 0
theorem Nat.factorial_dvd_factorial E1 #
∀ {m n : ℕ}, m ≤ n → m.factorial ∣ n.factorial
theorem Nat.dvd_factorial E1 #
∀ {m n : ℕ}, 0 < m → m ≤ n → m ∣ n.factorial
theorem Nat.factorial_le E1 #
∀ {m n : ℕ}, m ≤ n → m.factorial ≤ n.factorial
theorem Nat.factorial_mul_pow_le_factorial E1 #
∀ {m n : ℕ}, m.factorial * (m + 1) ^ n ≤ (m + n).factorial
theorem Nat.factorial_lt E1 #
∀ {m n : ℕ}, 0 < n → (n.factorial < m.factorial ↔ n < m)
theorem Nat.factorial_lt_of_lt E1 #
∀ {m n : ℕ}, 0 < n → n < m → n.factorial < m.factorial
theorem Nat.one_lt_factorial E0 #
∀ {n : ℕ}, 1 < n.factorial ↔ 1 < n
theorem Nat.factorial_eq_one E0 #
∀ {n : ℕ}, n.factorial = 1 ↔ n ≤ 1
theorem Nat.factorial_inj E1 #
∀ {m n : ℕ}, 1 < n → (n.factorial = m.factorial ↔ n = m)
theorem Nat.factorial_inj' E1 #
∀ {m n : ℕ}, 1 < n ∨ 1 < m → (n.factorial = m.factorial ↔ n = m)
theorem Nat.self_le_factorial E1 #
∀ (n : ℕ), n ≤ n.factorial
theorem Nat.lt_factorial_self E1 #
∀ {n : ℕ}, 3 ≤ n → n < n.factorial
theorem Nat.add_factorial_succ_lt_factorial_add_succ E1 #
∀ {i : ℕ} (n : ℕ), 2 ≤ i → i + (n + 1).factorial < (i + n + 1).factorial
theorem Nat.add_factorial_lt_factorial_add E1 #
∀ {i n : ℕ}, 2 ≤ i → 1 ≤ n → i + n.factorial < (i + n).factorial
theorem Nat.add_factorial_succ_le_factorial_add_succ E1 #
∀ (i n : ℕ), i + (n + 1).factorial ≤ (i + (n + 1)).factorial
theorem Nat.add_factorial_le_factorial_add E1 #
∀ (i : ℕ) {n : ℕ}, 1 ≤ n → i + n.factorial ≤ (i + n).factorial
theorem Nat.factorial_mul_pow_sub_le_factorial E1 #
∀ {n m : ℕ}, n ≤ m → n.factorial * n ^ (m - n) ≤ m.factorial
theorem Nat.factorial_le_pow E1 #
∀ (n : ℕ), n.factorial ≤ n ^ n
def Nat.ascFactorial E3 #
ℕ → ℕ → ℕ
n.ascFactorial k = n (n + 1) ⋯ (n + k - 1). This is closely related to ascPochhammer, but
much less general.
theorem Nat.ascFactorial_zero E0 #
∀ (n : ℕ), n.ascFactorial 0 = 1
theorem Nat.ascFactorial_succ E1 #
∀ {n k : ℕ}, n.ascFactorial k.succ = (n + k) * n.ascFactorial k
theorem Nat.zero_ascFactorial E1 #
∀ (k : ℕ), Nat.ascFactorial 0 k.succ = 0
theorem Nat.one_ascFactorial E0 #
∀ (k : ℕ), Nat.ascFactorial 1 k = k.factorial
theorem Nat.succ_ascFactorial E1 #
∀ (n k : ℕ), n * n.succ.ascFactorial k = (n + k) * n.ascFactorial k
theorem Nat.factorial_mul_ascFactorial E1 #
∀ (n k : ℕ), n.factorial * (n + 1).ascFactorial k = (n + k).factorial
(n + 1).ascFactorial k = (n + k) ! / n ! but without ℕ-division. See
Nat.ascFactorial_eq_div for the version with ℕ-division.
theorem Nat.factorial_mul_ascFactorial' E1 #
∀ (n k : ℕ), 0 < n → (n - 1).factorial * n.ascFactorial k = (n + k - 1).factorial
n.ascFactorial k = (n + k - 1)! / (n - 1)! for n > 0 but without ℕ-division. See
Nat.ascFactorial_eq_div for the version with ℕ-division. Consider using
factorial_mul_ascFactorial to avoid complications of ℕ-subtraction.
theorem Nat.ascFactorial_mul_ascFactorial E1 #
∀ (n l k : ℕ), n.ascFactorial l * (n + l).ascFactorial k = n.ascFactorial (l + k)
theorem Nat.ascFactorial_eq_div E1 #
∀ (n k : ℕ), (n + 1).ascFactorial k = (n + k).factorial / n.factorial
Avoid in favor of Nat.factorial_mul_ascFactorial if you can. ℕ-division isn't worth it.
theorem Nat.ascFactorial_eq_div' E1 #
∀ (n k : ℕ), 0 < n → n.ascFactorial k = (n + k - 1).factorial / (n - 1).factorial
Avoid in favor of Nat.factorial_mul_ascFactorial' if you can. ℕ-division isn't worth it.
theorem Nat.ascFactorial_of_sub E1 #
∀ {n k : ℕ}, (n - k) * (n - k + 1).ascFactorial k = (n - k).ascFactorial (k + 1)
theorem Nat.ascFactorial_le E1 #
∀ (k : ℕ) {n m : ℕ}, n ≤ m → n.ascFactorial k ≤ m.ascFactorial k
theorem Nat.pow_succ_le_ascFactorial E1 #
∀ (n k : ℕ), n ^ k ≤ n.ascFactorial k
theorem Nat.pow_lt_ascFactorial' E1 #
∀ (n k : ℕ), (n + 1) ^ (k + 2) < (n + 1).ascFactorial (k + 2)
theorem Nat.pow_lt_ascFactorial E1 #
∀ (n : ℕ) {k : ℕ}, 2 ≤ k → (n + 1) ^ k < (n + 1).ascFactorial k
theorem Nat.ascFactorial_le_pow_add E1 #
∀ (n k : ℕ), (n + 1).ascFactorial k ≤ (n + k) ^ k
theorem Nat.ascFactorial_le_factorial_mul_pow E1 #
∀ (n k : ℕ), n.ascFactorial k ≤ k.factorial * n ^ k
theorem Nat.ascFactorial_lt_pow_add E1 #
∀ (n : ℕ) {k : ℕ}, 2 ≤ k → (n + 1).ascFactorial k < (n + k) ^ k
theorem Nat.ascFactorial_pos E1 #
∀ (n k : ℕ), 0 < (n + 1).ascFactorial k
def Nat.descFactorial E3 #
ℕ → ℕ → ℕ
n.descFactorial k = n! / (n - k)! (as seen in Nat.descFactorial_eq_div), but
implemented recursively to allow for "quick" computation when using norm_num. This is closely
related to descPochhammer, but much less general.
theorem Nat.descFactorial_zero E0 #
∀ (n : ℕ), n.descFactorial 0 = 1
theorem Nat.descFactorial_succ E0 #
∀ (n k : ℕ), n.descFactorial (k + 1) = (n - k) * n.descFactorial k
theorem Nat.zero_descFactorial_succ E1 #
∀ (k : ℕ), Nat.descFactorial 0 (k + 1) = 0
theorem Nat.descFactorial_one E1 #
∀ (n : ℕ), n.descFactorial 1 = n
theorem Nat.succ_descFactorial_succ E1 #
∀ (n k : ℕ), (n + 1).descFactorial (k + 1) = (n + 1) * n.descFactorial k
theorem Nat.succ_descFactorial E1 #
∀ (n k : ℕ), (n + 1 - k) * (n + 1).descFactorial k = (n + 1) * n.descFactorial k
theorem Nat.descFactorial_self E1 #
∀ (n : ℕ), n.descFactorial n = n.factorial
theorem Nat.descFactorial_eq_zero_iff_lt E0 #
∀ {n k : ℕ}, n.descFactorial k = 0 ↔ n < k
theorem Nat.descFactorial_pos E0 #
∀ {n k : ℕ}, 0 < n.descFactorial k ↔ k ≤ n
theorem Nat.descFactorial_of_lt E1 #
∀ {n k : ℕ}, n < k → n.descFactorial k = 0
Alias of the reverse direction of Nat.descFactorial_eq_zero_iff_lt.
theorem Nat.descFactorial_mul_self E1 #
∀ (n j : ℕ), n.descFactorial j * n = n.descFactorial (j + 1) + j * n.descFactorial j
theorem Nat.add_descFactorial_eq_ascFactorial E1 #
∀ (n k : ℕ), (n + k).descFactorial k = (n + 1).ascFactorial k
theorem Nat.add_descFactorial_eq_ascFactorial' E1 #
∀ (n k : ℕ), (n + k - 1).descFactorial k = n.ascFactorial k
theorem Nat.factorial_mul_descFactorial E1 #
∀ {n k : ℕ}, k ≤ n → (n - k).factorial * n.descFactorial k = n.factorial
n.descFactorial k = n! / (n - k)! but without ℕ-division. See Nat.descFactorial_eq_div
for the version using ℕ-division.
theorem Nat.descFactorial_mul_descFactorial E1 #
∀ {k m n : ℕ}, k ≤ m → (n - k).descFactorial (m - k) * n.descFactorial k = n.descFactorial m
theorem Nat.descFactorial_eq_div E1 #
∀ {n k : ℕ}, k ≤ n → n.descFactorial k = n.factorial / (n - k).factorial
Avoid in favor of Nat.factorial_mul_descFactorial if you can. ℕ-division isn't worth it.
theorem Nat.descFactorial_le E1 #
∀ (n : ℕ) {k m : ℕ}, k ≤ m → k.descFactorial n ≤ m.descFactorial n
theorem Nat.pow_sub_le_descFactorial E1 #
∀ (n k : ℕ), (n + 1 - k) ^ k ≤ n.descFactorial k
theorem Nat.pow_sub_lt_descFactorial' E1 #
∀ {n k : ℕ}, k + 2 ≤ n → (n - (k + 1)) ^ (k + 2) < n.descFactorial (k + 2)
theorem Nat.pow_sub_lt_descFactorial E1 #
∀ {n k : ℕ}, 2 ≤ k → k ≤ n → (n + 1 - k) ^ k < n.descFactorial k
theorem Nat.descFactorial_le_pow E1 #
∀ (n k : ℕ), n.descFactorial k ≤ n ^ k
theorem Nat.descFactorial_lt_pow E1 #
∀ {n : ℕ}, n ≠ 0 → ∀ {k : ℕ}, 2 ≤ k → n.descFactorial k < n ^ k
theorem Nat.factorial_two_mul_le E1 #
∀ (n : ℕ), (2 * n).factorial ≤ (2 * n) ^ n * n.factorial
theorem Nat.two_pow_mul_factorial_le_factorial_two_mul E1 #
∀ (n : ℕ), 2 ^ n * n.factorial ≤ (2 * n).factorial
def Nat.ascFactorialBinary E1 #
ℕ → ℕ → ℕ
ascFactorial implemented using binary splitting.
While this still performs the same number of multiplications, the big-integer operands to each are much smaller.
theorem Nat.ascFactorial_eq_ascFactorialBinary E1 #
Nat.ascFactorial = Nat.ascFactorialBinary
def Nat.factorialBinarySplitting E1 #
ℕ → ℕ
Factorial implemented using binary splitting.
While this still performs the same number of multiplications, the big-integer operands to each are much smaller.
theorem Nat.factorial_eq_factorialBinarySplitting E1 #
Nat.factorial = Nat.factorialBinarySplitting
def Nat.descFactorialBinary E1 #
ℕ → ℕ → ℕ
descFactorial implemented using binary splitting.
theorem Nat.descFactorial_eq_descFactorialBinary E1 #
Nat.descFactorial = Nat.descFactorialBinary