何かを書き留める何か

数学や読んだ本について書く何かです。最近は社会人として生き残りの術を学ぶ日々です。

Project Euler Problem 24

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
http://projecteuler.net/problem=24

0から9までの数字を辞書式に並べたときの100万番目を求める問題。
高校数学の数学Aでよく出てきそうな問題である。
念のため確認しておくと、10!=3628800であるので100万番目は存在する。

Pythonならば簡単に求めることが出来る。

#Problem 24 Lexicographic permutations
import itertools
target = [c for c in itertools.permutations([0,1,2,3,4,5,6,7,8,9],10)]
ans = map(str,list(target[999999]))
print "".join(ans).strip() 

Pythonのリストは0番目からあることに注意。
10!通りすべてリストに入れなくてもよい気がしてきた。