From 5c4cec6c39b63404febd4084150bb059e72ae102 Mon Sep 17 00:00:00 2001 From: Jason Staten Date: Thu, 12 Dec 2019 21:08:12 -0700 Subject: [PATCH] erlang: cracking the coding interview 8.1 --- erlang/steps.erl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 erlang/steps.erl diff --git a/erlang/steps.erl b/erlang/steps.erl new file mode 100644 index 0000000..ad11dd3 --- /dev/null +++ b/erlang/steps.erl @@ -0,0 +1,14 @@ +-module(steps). +-export([permutations/1, combinations/1]). + +% Cracking the Coding Interview 8.1 +% Go up N steps 1, 2, or 3 at a time + +% Count of permutations +permutations(0) -> 0; +permutations(1) -> 1; +permutations(2) -> 2; +permutations(Steps) -> permutations(Steps - 1) + permutations(Steps - 2) + permutations(Steps - 3). + +% Brute force list of all combinations O(N^3) +combinations(N) -> [{X, Y, Z} || X <- lists:seq(0, N), Y <- lists:seq(0, N), Z <- lists:seq(0, N), (X + Y * 2 + Z * 3 == N)]. \ No newline at end of file