LoginSignup
2
3

More than 5 years have passed since last update.

二項定理をpythonで計算する

Last updated at Posted at 2018-10-25

二項定理の定義

二項定理とは,

初等代数学における二項定理(にこうていり、英: binomial theorem)または二項展開 (binomial expansion) は二項式の冪の代数的な展開を記述するものである。定理によれば、冪 (x + y)n は a xb yc の形の項の和に展開できる。ただし、冪指数 b, c は b + c = n を満たす非負整数で、各項の係数 a は n と b に依存して決まる特定の正整数である。

Wikipediaより

あっ…

Pythonに計算させよう

BinomialTheorem.py

from math import *

def calc(k):
    return [ factorial(k)/(factorial(i)*factorial(k-i)) for i in range(k+1)]

これを使うと,

>>> calc(0)
[1]

>>> calc(6)
[1, 6, 15, 20, 15, 6, 1]

>>> calc(12)
[1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1]

こんな感じでlist型で返してくれる.

2018/10/25訂正
関数calc内で結果をreturnしてませんでした.
@shiracamus さん,ご指摘いただきありがとうございます.

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3