2015-06-01から1ヶ月間の記事一覧

階乗

#!/usr/bin/env elixir defmodule Foo do def fact(n) do fact(n, 1) end defp fact(0, acc) do acc end defp fact(n, acc) do fact(n - 1, acc * n) end end IO.puts Foo.fact(65535) real 0m13.120s user 0m12.577s sys 0m0.201s

階乗

#!/usr/bin/env escript fact(0) -> 1; fact(N) -> N * fact(N - 1). main(_) -> N = fact(65535), io:format("~p~n", [N]). real 0m11.969s user 0m11.742s sys 0m0.175s #!/usr/bin/env escript fact(N) -> fact(N, 1). fact(0, Acc) -> Acc; fact(N, Acc)…

クイックソート

#!/usr/bin/env elixir defmodule QuickSort do def quick_sort([]) do [] end def quick_sort([pivot|rest]) do {smaller, larger} = partition(pivot, rest, [], []) quick_sort(smaller) ++ [pivot] ++ quick_sort(larger) end defp partition(_, [], sma…

クイックソート、リバース

#!/usr/bin/env escript print_list(List) -> io:format("len: ~p~n", [length(List)]), lists:foreach(fun(X) -> io:format("~p, ",[X]) end, List), io:format("~n"). quick_sort([]) -> []; quick_sort([Pivot|Rest]) -> {Smaller, Larger} = partition(P…