Skip to content

Instantly share code, notes, and snippets.

@fledman
Created May 1, 2015 19:53
Show Gist options
  • Save fledman/bbc94017a468d240d520 to your computer and use it in GitHub Desktop.
Save fledman/bbc94017a468d240d520 to your computer and use it in GitHub Desktop.
Lexicographic permutations
# 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