2 $log = Logger
.new(STDERR)
3 $log.level
= Logger
::WARN
8 return [0] if self == 0
13 result
<< (remainder
% radix
)
14 remainder
= remainder
.div radix
22 def factorial_radix_to_i(current_radix
= 0)
23 $log.info
{ "Calling factorial_radix_to_i with string #{self} and current_radix = #{current_radix}" }
24 return 0 if self.empty
?
25 return 0 if self == [0]
26 value_of_rest
= self[0..-2].factorial_radix_to_i(current_radix
+ 1)
27 $log.info
{ "Value of remainder = #{value_of_rest}" }
28 $log.info
{ "Returning #{self[-1] + value_of_rest}" }
29 self[-1] + (current_radix
+ 1) * value_of_rest
32 def prepad_with_zeros(length
)
33 [0] * [length
- self.length
, 0].max
+ self
36 def permutation_from_factorial_radix_number(n
)
37 $log.info
{ "Permuting #{self} with frn #{n}" }
39 return self if self.empty
?
41 permutation
= [items_copy
.delete_at(n
[0])]
42 permutation
+= items_copy
.permutation_from_factorial_radix_number(n
[1..-1])
45 def nth_permutation(n
)
46 permutation_from_factorial_radix_number(n
.to_factorial_radix
.prepad_with_zeros(self.length
))
51 puts (Array
.new(10) {|i
| i
}).nth_permutation(10 ** 6 - 1).join('')