Last active
August 29, 2015 14:24
-
-
Save kingluo/557e47518809f3654636 to your computer and use it in GitHub Desktop.
math example: combination
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
-module(perm). | |
-export([main/1]). | |
iend(M, N, Level) -> | |
Room = N - Level, | |
M - Room. | |
for(S, E, F) when S =< E -> F(S), for(S + 1, E, F); | |
for(_, _, _) -> void. | |
test(L, M, N, T, Cur, Level) when M >= N -> | |
for(Cur, iend(M, N, Level), fun(I) -> | |
T2 = [lists:nth(I, L) | T], | |
if | |
Level =/= N -> test(L, M, N, T2, I + 1, Level + 1); | |
true -> io:format("~p~n", [lists:reverse(T2)]) | |
end | |
end). | |
main([]) -> | |
L = [1,2,3,4,5,6], | |
test(L, length(L), 3, [], 1, 1). |
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
local L = {1,2,3,4,5,6} | |
local M = #L | |
local N = 3 | |
function iend(M, N, level) | |
local room = N - level | |
return M - room | |
end | |
function dup(T) | |
return {table.unpack(T)} | |
end | |
function test(L, M, N, T, cur, level) | |
T = T or {} | |
cur = cur or 1 | |
level = level or 1 | |
for i = cur, iend(M, N, level) do | |
T[level] = L[i] | |
if level ~= N then | |
test(L, M, N, dup(T), i + 1, level + 1) | |
else | |
for i, v in ipairs(T) do | |
io.write(v) | |
io.write("\t") | |
end | |
print"" | |
end | |
end | |
end | |
print"" | |
test(L, M, N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment