Hovedinnhold
Kurs: (Informatikk > Enhet 1
Leksjon 6: Rekursive algoritmer- Rekursjon
- Fraktal funksjoner
- Utfordring: Iterative fakultet
- Rekursiv fakultet
- Utfordring: Rekursiv fakultet
- Egenskaper for rekursive algoritmer
- Å bruke rekursjon for å avgjøre om et ord er en palindrome (er ord som er det samme forlengs og baklengs)
- Utfordringen: er en linje en palindrome?
- Generere potensen av et tall
- Utfordring: Rekursive potenser
- The Sierpinski gasket
- Prosjekt: Rekursiv kunst
© 2024 Khan AcademyBrukervilkårPersonvernVarsel om informasjonskapsler
Fraktal funksjoner
For our first example of recursion, let's look at how to compute the factorial function. We indicate the factorial of by . It's just the product of the integers 1 through . For example, 5! equals , or 120. (Note: Wherever we're talking about the factorial function, all exclamation points refer to the factorial function and are not for emphasis.)
You might wonder why we would possibly care about the factorial function. It's very useful for when we're trying to count how many different orders there are for things or how many different ways we can combine things. For example, how many different ways can we arrange things? We have choices for the first thing. For each of these choices, we are left with choices for the second thing, so that we have choices for the first two things, in order. Now, for each of these first two choices, we have choices for the third thing, giving us choices for the first three things, in order. And so on, until we get down to just two things remaining, and then just one thing remaining. Altogether, we have ways that we can order things. And that product is just ( factorial), but with the product written going from down to 1 rather than from 1 up to .
Another use for the factorial function is to count how many ways you can choose things from a collection of things. For example, suppose you are going on a trip and you want to choose which T-shirts to take. Let's say that you own T-shirts but you have room to pack only of them. How many different ways can you choose T-shirts from a collection of T-shirts? The answer (which we won't try to justify here) turns out to be . So the factorial function can be pretty useful. You can learn more about permutations and combinations here, but you don't need to understand them to implement a factorial algorithm.
The factorial function is defined for all positive integers, along with 0. What value should 0! have? It's the product of all integers greater than or equal to 1 and less than or equal to 0. But there are no such integers. Therefore, we define 0! to equal the identity for multiplication, which is 1. (Defining 0! = 1 meshes nicely with the formula for choosing things out of things. Suppose that we want to know how many ways there are to choose things out of things. That's easy, because there is only one way: choose all things. So now we know that, using our formula, should equal 1. But is 0!, so now we know that should equal 1. If we cancel out the in both the numerator and denominator, we see that should equal 1, and it does because 0! equals 1.)
So now we have a way to think about . It equals 1 when , and it equals when is positive.
This content is a collaboration of Dartmouth Computer Science professors Thomas Cormen and Devin Balkcom, plus the Khan Academy computing curriculum team. The content is licensed CC-BY-NC-SA.
Ønsker du å delta i samtalen?
Ingen innlegg enda.