Recursion Flashcards
Recursion
What are the 2 types of cases that all recursive algorithms must have?
- Base case (return)
- Recursive case (call the function again)
When are recursive algorithms useful?
When dealing with data structures that have unknown dephs (e.g. searching through a binary tree or file system)
What is a factorial and what is it used for?
Factorials determine the number of possible combinations (permutations) of n
More specifically, a factorial of n is the product of all positive integers less than or equal to n. However, it is important to remember the factorial of 0 is 1 and not 0. This is because the possible combinations of 0 is no combination which counts as a combination itself
if n = 0, n! = 1 if n >= 1, n! = n * (n-1)!
True or False
Any iterative algorithm can be written recursively
True
Code a function that computes a fibonacci sequence recursively