Skip to content

Instantly share code, notes, and snippets.

@mosinski
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosinski/7e60593eefc3991324a7 to your computer and use it in GitHub Desktop.
Save mosinski/7e60593eefc3991324a7 to your computer and use it in GitHub Desktop.
def PERM(n)
puts "#{@my_array[1..-1].inspect}"
for i in 1..(1..n).inject(:*)
a = 0
loop do
if a > 1 && a < n+1
temp = @temp_array[a]
if a - temp > 1
@temp_array[a] = temp + 1;
@my_array[B(a,temp+1)], @my_array[a] = @my_array [a], @my_array[B(a,temp+1)]
puts "#{@my_array[1..-1].inspect}"
break
else
@temp_array[a] = 0;
end
elsif a > n
break
end
a = a+1;
end
end
end
def B(n,i)
if (n % 2 == 0) & (n > 2)
if i < n - 1
i = i
else
n = n - 2
end
else
n = n - 1
end
end
n = ARGV[0].to_i
@my_array = Array(0..n)
@temp_array = Array.new(n+1,0)
PERM(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment