You are viewing a single comment's thread. Return to all comments →
#Simple Ruby solution word = 'abcdefghijklm' def factorial(n) return n <= 1 ? 1 : n*factorial(n-1) end t = gets.strip.to_i for a0 in (0..t-1) n = gets.strip.to_i letters = word.split(//) permutation = [] letter_count = letters.size k = factorial(letter_count) letter_count.downto(1) { |num_letters| k /= num_letters offset = (n-1)/k permutation << letters[offset] letters.delete_at(offset) n -= (k*offset) } puts permutation.join end
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #24: Lexicographic permutations
You are viewing a single comment's thread. Return to all comments →