~statianzo/sevenlangs

sevenlangs/erlang/steps.erl -rw-r--r-- 496 bytes
2fcd4511Jason Staten day one logic style 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
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)].