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?

解法

階乗の各桁で割って行った数の番目の数字を連結したら出来上がり!

ソース

class Integer
	def fact
		return 1 if self.zero?
		(1..self).inject(:*)
	end
end

x = 1000000 - 1
str = "0123456789".scan(/./)
puts (0...10).to_a.reverse.map{|i|
	[x / i.fact, x %= i.fact][0]
}.map{|i| str.delete_at(i)}.join

解答

2783915460

感想

やっと少し骨のある問題になって来た感じ!
風呂の中で15分位考えたら「なんとなくこんな感じでできるのかな?」で出来た
勘も必要になってくるな。。
でも、今回のソースは上出来!!delete_atが上手く使えた
(半分ショートコーディングになりかけてるwww)