Skip to content

Instantly share code, notes, and snippets.

@tomshaw
Created January 8, 2020 22:36
Show Gist options
  • Save tomshaw/7c15bf103d9c378719fb3cb84eae1b20 to your computer and use it in GitHub Desktop.
Save tomshaw/7c15bf103d9c378719fb3cb84eae1b20 to your computer and use it in GitHub Desktop.
Find combinations of input value that can win 1st, 2nd, 3rd prize.
function combinations(n) {
if (n < 0) {
return 0;
} else if (n < 3) {
return n;
}
return n * (n - 1) * (n - 2);
}
console.log(combinations(8)) // (8 - 1 = 7) * (8 - 2 = 6) = (42 * 8) = 336
console.log(combinations(6)) // (6 - 1 = 5) * (6 - 2 = 4) = (20 * 6) = 120
console.log(combinations(4)) // (4 - 1 = 3) * (4 - 2 = 2) = (6 * 4) = 24
console.log(combinations(3)) // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment