What is a good example of Recursion (other than generating a Fibonacci sequence)?
What is a good example of Recursion (other than generating a Fibonacci sequence)?
Here are some good examples of recursion other than generating the Fibonacci sequence:
The Towers of Hanoi is a classic recursion problem. The goal is to move a stack of discs from one pole to another, following these rules:
To solve this recursively:
The factorial of a number n is the product of all positive integers less than or equal to n. It can be defined recursively as:
factorial(n) = 1 if n = 0
= n * factorial(n-1) if n > 0
So the factorial of 5 would be calculated recursively as:
factorial(5) = 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
= 5 * 4 * 3 * 2 * 1 = 120
```[2][4]
## Palindrome Checker
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. A recursive palindrome checker can be implemented by:
1. Co...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào