Let's make a combination calculation in Python

Problem awareness

In Previous article, I recursively performed combination calculation in Python, but I thought that there are other ways.

Review of combinations

This is the combination that I learned from the permutation combination.

_nC_r=\frac{n!}{r!(n-r)!}

It was. Here, when n <r, the combination is 0, that is,

_0C_1=0

I promise 0 if it is out of the definition.

Like a recurrence formula

This time, I want to adopt the method of recursively calling the function with Python, so I will prepare something like a recurrence formula.

Recurrence formula 1: Decrease r

_nC_r=_nC_{r-1}\times\frac{n-r+1}{r}

Proof

First, from the definition

_nC_r=\frac{n!}{r!(n-r)!}=\frac{n!}{r\times(r-1)!\times(n-r)!}=\frac{n!}{(r-1)!\times(n-r)!}\times\frac{1}{r}\\
_nC_{r-1}=\frac{n!}{(r-1)!\times(n-r+1)\times(n-r)!}=\frac{n!}{(r-1)!\times(n-r)!}\times\frac{1}{n-r+1}

Therefore

r\times_nC_r=(n-r+1)_nC_{r-1}

Divide both sides by r to get the formula to show ■

Recurrence formula 2: Decrease n

_nC_r=_{n-1}C_{r-1}+_{n-1}C_r

This is self-explanatory considering Pascal's triangle, but I will not go too deeply-

Proof

\begin{eqnarray}
_nC_r+_nC_{r-1} &=& \frac{n!}{r!(n-r)!}+\frac{n!}{(r-1)!(n-r+1)!}\\
&=& \frac{n!\times\{(n-r+1)+r\}}{r!(n-r+1)!}\\
&=& \frac{n!\times(n+1)}{r!(n-r+1)!}=\frac{(n+1)!}{r!(n+1-r)!}\\
&=& _{n+1}C_r
\end{eqnarray}

Write in Python

Recurrence formula 1

_nC_r=_nC_{r-1}\times\frac{n-r+1}{r}

Based on, as the edge, that is, as the initial value setting

_0C_r=_nC_0=1

When written recursively as a function to return, it looks like this ([previous article](http://qiita.com/kyoro1/items/eed40ab333a9f9fd8ede#python%E3%81%A7%E3%82%B0%E3%] 83% A9% E3% 83% 95% E3% 82% 92% E6% 8F% 8F% E3% 81% 84% E3% 81% A6% E3% 81% BF% E3% 82% 8B) Same as)

def comb1(n, r):
    if n == 0 or r == 0: return 1
    return comb1(n, r-1) * (n-r+1) / r 

Recurrence formula 2

_nC_r=_{n-1}C_{r-1}+_{n-1}C_r

The following is the initial value set based on:

def comb2(n,r):
    if n < 0 or r < 0 or n < r: 
        return 0
    elif n == 0 or r == 0: 
        return 1
    return comb2(n-1,r-1)+comb2(n-1,r)

The reason why the initial value setting is a little more complicated than the recurrence formula 1 is that both n and r change.

Try out

Let's move it immediately.

>>> comb1(20,5)
15504.0
>>> comb2(20,5)
15504

Check with existing scipy function

>>> import scipy.misc as scm
>>> scm.comb(20, 5)
15504.0

→ Yeah, that's right ♪ (It's not proof at all, but w)

The site that I used as a reference

-Calculate Combination in Python

Recommended Posts

Let's make a combination calculation in Python
Make a bookmarklet in Python
Let's make a GUI with python.
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Date calculation in Python
Make a copy of the list in Python
Make a rock-paper-scissors game in one line (python)
Let's make some notification processing samples in Python
Make a joyplot-like plot of R in python
Let's make a module for Python using SWIG
Let's use def in python
Let's make a Discord Bot.
Try to make a Python module in C language
Create a function in Python
Create a dictionary in Python
I want to write in Python! (2) Let's write a test
Shapley value calculation in Python
Make a simple Slackbot with interactive button in python
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
[For play] Let's make Yubaba a LINE Bot (Python)
Make a fortune with Python
Make Opencv available in Python
Draw a heart in Python
Let's make a rock-paper-scissors game
[Super easy] Let's make a LINE BOT with Python.
Let's find pi in Python
Let's make a cron program in Java! !! (Task Scheduler)
Let's make a websocket client with Python. (Access token authentication)
Make a table of multiplication of each element in a spreadsheet (Python)
Let's create a script that registers with Ideone.com in Python.
Let's make a number guessing game in your own language!
I tried to make a stopwatch using tkinter in python
I want to make input () a nice complement in python
Let's make a remote rumba [Hardware]
Maybe in a python (original title: Maybe in Python)
[python] Manage functions in a list
Let's make a remote rumba [Software]
Quantum chemistry calculation in Python (Psi4)
Let's run "python -m antigravity" in python
Create a DI Container in Python
Let's make a spot sale service 2
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Accelerometer Alan Variance Calculation in Python
Let's make a breakout with wxPython
Write A * (A-star) algorithm in Python
Let's make a spot sale service 1
Let's try Fizz Buzz in Python
Make standard output non-blocking in Python
Create a binary file in Python
python / Make a dict from a list.
[Python] Make the function a lambda function
Make a recommender system with python
Solve ABC036 A ~ C in Python
Write a pie chart in Python