Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Hi everyone!
In this blog post, I'm going to give you an introduction to recursion. Recursion
is a technique in computer science where a function calls itself to solve a
problem. Let's take a look at a few examples to understand how it works.
Factorials
Let's quickly review factorials here. If you're given a positive integer n, n! is
equal to n times n-1 times n-2 times n-3 down to 3 times 2 times.
1. Let's look at a few examples:
4 factorial (4!) is equal to 4 times 3 times 2 times 1, which is equal to 24.
2 factorial (2!) is equal to 2 times 1, which is 2.
1 factorial (1!) is equal to 1.
What about 0 factorial (0!)? It's actually defined to be 1. You might wonder
why, but this is just how it is defined.
CS Dojo
Hi everyone!
In this blog post, I'm going to give you an introduction to recursion. Recursion
is a technique in computer science where a function calls itself to solve a
problem. Let's take a look at a few examples to understand how it works.
Factorials
Let's quickly review factorials here. If you're given a positive integer n, n! is
equal to n times n-1 times n-2 times n-3 down to 3 times 2 times.
1. Let's look at a few examples:
4 factorial (4!) is equal to 4 times 3 times 2 times 1, which is equal to 24.
2 factorial (2!) is equal to 2 times 1, which is 2.
1 factorial (1!) is equal to 1.
What about 0 factorial (0!)? It's actually defined to be 1. You might wonder
why, but this is just how it is defined.