2014年10月27日月曜日

141027

Python


Partition (number theory)

140823(3)のPython版。

n = 6
ps = [0 for i in xrange(n + 1)]
ps[0] = 1
for j in xrange(1,n + 1):
    for i in xrange(j,n + 1):
        ps[i] += ps[i - j]
        
print ps[n]

2014年10月20日月曜日

141020

Ruby


区分求積法

会社に行く前に以下の問題にトライしてみた。
https://codeiq.jp/ace/cielavenir/q1120

「y = x^x を x で 1 から 6 まで積分した値を

 1から 6 までを n 分割したものを
 ・台形公式(Trapezoid rule)  
 ・シンプソンの公式(Simpson's rule)  
 ・シンプソンの3/8公式(Simpson's 3/8 rule)  
 ・ブールの公式(Boole's rule)

 で近似する。

 真の値 17128.111274826415512 から0.1未満の誤差にするには
 少なくとも何分割必要か求めよ」
という問題。

実質1時間しかなく台形の場合しか正解しなかったが、
ようやく答えがあったのでコードを晒しておく。

def f(i)     # i番目の高さ
  x = 1 + (i - 1) * @d
  x ** x
end

n = 5
@d = (6.0 - 1.0) / n
sum = 0
for j in (1..n)
  sum += (f(j) + f(j + 1)) / 2 * @d
end
while sum - 17128.111274826415512 >= 0.1
  n += 1
  @d = (6.0 - 1.0) / n
  sum = 0
  for j in (1..n)
    sum += (f(j) + f(j + 1)) / 2 * @d
  end
end
p n

n = 5
@d = (6.0 - 1.0) / (n * 2)
sum = 0
for j in (1..n)
  sum += (f(j * 2 - 1) + 4 * f(j * 2) + f(j * 2 + 1)) / 3 * @d
end
while sum - 17128.111274826415512 >= 0.1
  n += 1
  @d = (6.0 - 1.0) / (n * 2)
  sum = 0
  for j in (1..n)
    sum += (f(j * 2 - 1) + 4 * f(j * 2) + f(j * 2 + 1)) / 3 * @d
  end
end
p n

n = 5
@d = (6.0 - 1.0) / (n * 3)
sum = 0
for j in (1..n)
  sum += (f(j * 3 - 2) + 3 * f(j * 3 - 1) + 3 * f(j * 3) + f(j * 3 + 1)) * 3 / 8 * @d
end
while sum - 17128.111274826415512 >= 0.1
  n += 1
  @d = (6.0 - 1.0) / (n * 3)
  sum = 0
  for j in (1..n)
    sum += (f(j * 3 - 2) + 3 * f(j * 3 - 1) + 3 * f(j * 3) + f(j * 3 + 1)) * 3 / 8 * @d
  end
end
p n

n = 5
@d = (6.0 - 1.0) / (n * 4)
sum = 0
for j in (1..n)
  sum += (7 * f(j * 4 - 3) + 32 * f(j * 4 - 2) + 12 * f(j * 4 - 1) + 32 * f(j * 4) + 7 * f(j * 4 + 1)) * 2 / 45 * @d
end
while sum - 17128.111274826415512 >= 0.1
  n += 1
  @d = (6.0 - 1.0) / (n * 4)
  sum = 0
  for j in (1..n)
    sum += (7 * f(j * 4 - 3) + 32 * f(j * 4 - 2) + 12 * f(j * 4 - 1) + 32 * f(j * 4) + 7 * f(j * 4 + 1)) * 2 / 45 * @d
  end
end
p n

出力結果
1648
40
32
10

2014年10月19日日曜日

141019

Python


複数行のコメントの仕方

""" 
ここは 
複数行コメント
"""

2014年10月18日土曜日

141018

Python


140511と以下を比較して、
Pythonはコードの短さについては言及していないのかな…と思った。

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2014年10月5日日曜日

141005

Python


タートルグラフィックス

turtle.xcor()
タートルの x 座標を返す。

turtle.ycor()
タートルの y 座標を返す。

2014年10月4日土曜日

141004

Python


今日の夕方からPythonを勉強し始める。
新しいことばかりだったので、忘れないうちに記しておくことにする。

インストールについて
DLしたのはいいが、そのままだと上手くインストールできず。
デスクトップにファイルを移動後、インストール。

ファイルの開き方
pyファイルはEdit with IDLEで開かないと一瞬で開いた画面が消える。

ファイルの保存
エディタはRubyのときからTeraPadを使用しているが、
ファイルの種類は「すべてのファイル」を選択し、
名前を「〜.py」にする。