Project Euler Problem 41
問題
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists?
ソース
class Integer def prime? self >= 2 && (2..Math.sqrt(self)).all?{|n| !(self % n).zero?} end end max = 0 (1..9).each do |l| (1..l).to_a.permutation(l).each do |pand| n = pand.join.to_i max = n if n.prime? end end puts max
解答
7652413
感想
全探索だけど、そんなに時間がかからないです。
permutationで全てに組み合わせを生成してjoin.to_iしています。(よく使う手です