Created
May 1, 2015 19:53
-
-
Save fledman/bbc94017a468d240d520 to your computer and use it in GitHub Desktop.
Lexicographic permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Project Euler: Problem 24 | |
# Lexicographic permutations | |
# https://projecteuler.net/problem=24 | |
def fact(x); (1..x).reduce(1,:*); end | |
l=(0..9).to_a | |
out = [] | |
marker = 1_000_000 | |
while !l.empty? do | |
puts marker | |
m,ind = l.each_with_index.find{ |x,i| (i+1) * fact(9-out.size) >= marker } | |
marker -= ind * fact(9-out.size) | |
out << l.delete(m) | |
end | |
out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment