Function of #cas

binomial

The binomial coefficient (nk) of natural numbers n and k, with a theorem about Mathlib’s Nat.choose.

#cas (binomial n k)
gives the binomial coefficient (nk) of the natural numbers n and k and proves Nat.choose n k = c.

binomial computes a binomial coefficient (nk). It is a small thing, but the value comes with a theorem about Mathlib’s Nat.choose, so a proof can use the number the report prints instead of a number copied over by hand.

Details and Options

  • n and k are natural numbers, literals or closed integer expressions such as 2 ^ 10. Negative and non-integer arguments are refused, since Nat.choose is defined on natural numbers.
  • The theorem is Nat.choose n k = c. For k≤n the proof rewrites with Nat.choose_eq_descFactorial_div_factorial, after Nat.choose_symm when n−k<k, and norm_num evaluates n(n−1)⋯(n−j+1)/j! for the smaller j of k and n−k.
  • For k>n the proof applies Nat.choose_eq_zero_of_lt, and the value is 0.
  • 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 (9)#

Basic Examples (1)

The number of ways to choose 3 of 10 things, with the theorem that the kernel checked:

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

binomial

120
Domain
ℕ
Input (natural numbers)
(10, 3)

Checked by Lean’s kernel: binomial

Nat.choose 10 3 = 120
The certificate: 1 checked step
  1. binomial

    Nat.choose 10 3 = 120

    Checked by Lean’s kernel

Scope (2)

The largest coefficient for n=100, with 30 digits:

In[2]:=
#cas (binomial 100 50)
Out[2]=

binomial

100891344545564193334812497256
Domain
ℕ
Input (natural numbers)
(100, 50)

Checked by Lean’s kernel: binomial

Nat.choose 100 50 = 100891344545564193334812497256
The certificate: 1 checked step
  1. binomial

    Nat.choose 100 50 = 100891344545564193334812497256

    Checked by Lean’s kernel


For k>n the coefficient is 0:

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

binomial

0
Domain
ℕ
Input (natural numbers)
(3, 10)

Checked by Lean’s kernel: binomial

Nat.choose 3 10 = 0
The certificate: 1 checked step
  1. binomial

    Nat.choose 3 10 = 0

    Checked by Lean’s kernel

Applications (1)

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

In[4]:=
theorem choose_ten_three : Nat.choose 10 3 = 120 := by
  cas

#print axioms choose_ten_three
Out[4]=

choose_ten_three depends on no axioms.

Properties and Relations (1)

For k≤n, (nk)=n!k!(n−k)!. With 10!=3628800 from factorial, (103)=36288006·5040=120:

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

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

Possible Issues (4)

Mathlib’s Ring.choose extends (nk) to negative n, and the call evaluates Nat.choose, so a negative argument is refused:

In[6]:=
#cas (binomial (-3) 2)
Out[6]=

Error

binomial takes a natural number as its first argument; `-3` is negative

The arguments are numbers, not variables:

In[7]:=
#cas (binomial x 2)
Out[7]=

Error

binomial takes integer arguments; `x` is a variable

The call takes two arguments:

In[8]:=
#cas (binomial 10)
Out[8]=

Error

binomial takes 2 arguments, as in `binomial n k`; the call has 1

The value has at most 10000 digits, and (10000050000) has 30101:

In[9]:=
#cas (binomial 100000 50000)
Out[9]=

Error

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

See Also

factorial · gamma · integerDigits · cas