Function of #cas

factorial

The factorial n! of a natural number, with a theorem about Mathlib’s Nat.factorial.

#cas (factorial n)
gives the factorial n!=1·2⋯n of the natural number n and proves Nat.factorial n = f.

factorial computes n! and proves the value of Mathlib’s Nat.factorial. It’s a small call, but the number you see is the number Mathlib means, so a proof can use it as it stands. When you need the factorial beyond the natural numbers, gamma extends it, with Γ(n+1)=n!.

Details and Options

  • n is a natural number, a literal or a closed integer expression, and 0!=1.
  • The theorem is Nat.factorial n = f. norm_num proves it with Mathlib’s extension for factorials (Mathlib.Tactic.NormNum.NatFactorial), which splits the product in halves, and decide +kernel is the fallback.
  • An argument with a type ascription, (a : ℕ) or (a : ℤ), is evaluated with Lean’s arithmetic of that type. Other ascriptions are refused. Without an ascription the argument is read as the calculator reads its input, and its value has to be an integer.
  • The value has at most 10000 digits, the most that a call computes.
  • The call is evaluated when it is the whole input of #cas.
  • There are no options.

Examples (11)#

Basic Examples (1)

Ten factorial, with the theorem that the kernel checked:

In[1]:=
#cas (factorial 10)
Out[1]=

factorial

3628800
Domain
ℕ
Input (natural numbers)
10

Checked by Lean’s kernel: factorial

Nat.factorial 10 = 3628800
The certificate: 1 checked step
  1. factorial

    Nat.factorial 10 = 3628800

    Checked by Lean’s kernel

Scope (4)

A value with 33 digits:

In[2]:=
#cas (factorial 30)
Out[2]=

factorial

265252859812191058636308480000000
Domain
ℕ
Input (natural numbers)
30

Checked by Lean’s kernel: factorial

Nat.factorial 30 = 265252859812191058636308480000000
The certificate: 1 checked step
  1. factorial

    Nat.factorial 30 = 265252859812191058636308480000000

    Checked by Lean’s kernel


The empty product, 0!=1:

In[3]:=
#cas (factorial 0)
Out[3]=

factorial

1
Domain
ℕ
Input (natural numbers)
0

Checked by Lean’s kernel: factorial

Nat.factorial 0 = 1
The certificate: 1 checked step
  1. factorial

    Nat.factorial 0 = 1

    Checked by Lean’s kernel


In ℕ the subtraction 1−2 is truncated to 0, so the argument is 3:

In[4]:=
#cas (factorial ((1 - 2 : ℕ) + 3))
Out[4]=

factorial

6
Domain
ℕ
Input (natural numbers)
3

Checked by Lean’s kernel: factorial

Nat.factorial 3 = 6
The certificate: 1 checked step
  1. factorial

    Nat.factorial 3 = 6

    Checked by Lean’s kernel


In ℤ the subtraction gives −1, and the argument is 2:

In[5]:=
#cas (factorial ((1 - 2 : ℤ) + 3))
Out[5]=

factorial

2
Domain
ℕ
Input (natural numbers)
2

Checked by Lean’s kernel: factorial

Nat.factorial 2 = 2
The certificate: 1 checked step
  1. factorial

    Nat.factorial 2 = 2

    Checked by Lean’s kernel

Applications (1)

In a proof, cas proves the equation, here without any axioms:

In[6]:=
theorem factorial_ten : Nat.factorial 10 = 3628800 := by
  cas

#print axioms factorial_ten
Out[6]=

factorial_ten depends on no axioms.

Properties and Relations (2)

Γ(n+1)=n!, so gamma at 5 gives 4!=24:

In[7]:=
#cas (gamma 5)
Out[7]=

exact value of Γ

24
Input (rational number)
5

Checked by Lean’s kernel: Gamma by a closed formula

Real.Gamma 5 = 24
The certificate: 1 checked step
  1. Gamma by a closed formula

    Real.Gamma 5 = 24

    Checked by Lean’s kernel


Γ extends the factorial to the half-integers, with Γ(72)=52·32·12·π:

In[8]:=
#cas (gamma (7/2))
Out[8]=

exact value of Γ

15π8
15√π/8
Input (rational number)
7/2

Checked by Lean’s kernel: Gamma by a closed formula

Real.Gamma (7 / 2) = 15 * √Real.pi / 8
The certificate: 1 checked step
  1. Gamma by a closed formula

    Real.Gamma (7 / 2) = 15 * √Real.pi / 8

    Checked by Lean’s kernel

Possible Issues (3)

Nat.factorial is defined on natural numbers, and a negative argument is refused:

In[9]:=
#cas (factorial (-1))
Out[9]=

Error

factorial takes a natural number as its argument; `-1` is negative

The ascriptions of an integer argument are ℕ and ℤ, and the ascription ℝ is refused even when the value is an integer:

In[10]:=
#cas (factorial (3 : ℝ))
Out[10]=

Error

factorial takes integer arguments; `(3 : ℝ)` has the type `ℝ`, and the ascriptions of an integer argument are `ℕ` and `ℤ`

The value has at most 10000 digits, and 5000! has 16326:

In[11]:=
#cas (factorial 5000)
Out[11]=

Error

factorial: the value has more than 10000 digits, the most that a call computes

See Also

binomial · gamma · fibonacci · cas