You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
14 lines
496 B
14 lines
496 B
-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)]. |