Skip to content

Instantly share code, notes, and snippets.

@the-vampiire
Created September 29, 2017 18:51
Show Gist options
  • Select an option

  • Save the-vampiire/4e31648455a961b5f14952c3cea2c8ea to your computer and use it in GitHub Desktop.

Select an option

Save the-vampiire/4e31648455a961b5f14952c3cea2c8ea to your computer and use it in GitHub Desktop.
algo solution for sup
function conditionalRecursion(n) {
if (n < 2) return n;
return conditionalRecursion(n - 1) + conditionalRecursion(n - 2);
}
const tests = [
[1, 3, 5, 7],
[0, 1, 1, 2],
[2, 4, 6, 8],
[0, 1, 2, 3],
];
function testRecursion(tests) {
tests.forEach((test, index) => {
test.forEach(n => console.log(`test ${index} input: ${n} output: ${conditionalRecursion(n)}`));
});
}
// run the script to get the outputs
testRecursion(tests);
@the-vampiire
Copy link
Author

Outputs:

test 0 input: 1 output: 1
test 0 input: 3 output: 2
test 0 input: 5 output: 5
test 0 input: 7 output: 13
test 1 input: 0 output: 0
test 1 input: 1 output: 1
test 1 input: 1 output: 1
test 1 input: 2 output: 1
test 2 input: 2 output: 1
test 2 input: 4 output: 3
test 2 input: 6 output: 8
test 2 input: 8 output: 21
test 3 input: 0 output: 0
test 3 input: 1 output: 1
test 3 input: 2 output: 1
test 3 input: 3 output: 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment