### Title: Simple Recursion Challenges in JavaScript, Ruby, and Python
### Description:
This article explores the concept of recursion through simple challenges implemented in three popular programming languages: JavaScript, Ruby, and Python. It provides code snippets and explanations to help readers understand how recursion works in each language.
### Content:
#### Introduction to Recursion
Recursion is a powerful programming technique where a function calls itself repeatedly until it reaches a base case that stops the recursion. This article will showcase simple recursive challenges in JavaScript, Ruby, and Python, illustrating how recursion can be applied in practical scenarios.
#### Simple Recursive Challenges
##### 1. Factorial Calculation
Factorials are a classic example of recursive functions. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
**JavaScript Implementation**
```javascript
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5)); // Output: 120
```
**Ruby Implementation**
```ruby
def factorial(n)
return 1 if n == 0 || n == 1
n * factorial(n - 1)
end
puts factorial(5) # Output: 120
```
**Python Implementation**
```python
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
```
##### 2. Fibonacci Sequence
The Fibonacci sequence is another common use case for recursion. Each number in the sequence is the sum of the two preceding ones, usually starting with 0 and 1.
**JavaScript Implementation**
```javascript
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10)); // Output: 55
```
**Ruby Implementation**
```ruby
def fibonacci(n)
return 0 if n == 0
return 1 if n == 1
fibonacci(n - 1) + fibonacci(n - 2)
end
puts fibonacci(10) # Output: 55
```
**Python Implementation**
```python
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10)) # Output: 55
```
##### 3. Tower of Hanoi
The Tower of Hanoi is a classic problem that involves moving a stack of disks from one peg to another using a third peg, adhering to specific rules.
**JavaScript Implementation**
```javascript
function towerOfHanoi(n, source, target, auxiliary) {
if (n === 1) {
console.log(`Move disk 1 from ${source} to ${target}`);
return;
}
towerOfHanoi(n - 1, source, auxiliary, target);
console.log(`Move disk ${n} from ${source} to ${target}`);
towerOfHanoi(n - 1, auxiliary, target, source);
}
towerOfHanoi(3, 'A', 'C', 'B');
```
**Ruby Implementation**
```ruby
def tower_of_hanoi(n, source, target, auxiliary)
return if n == 1
tower_of_hanoi(n - 1, source, auxiliary, target)
puts "Move disk #{n} from #{source} to #{target}"
tower_of_hanoi(n - 1, auxiliary, target, source)
end
tower_of_hanoi(3, 'A', 'C', 'B')
```
**Python Implementation**
```python
def tower_of_hanoi(n, source, target, auxiliary):
if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n - 1, source, auxiliary, target)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n - 1, auxiliary, target, source)
tower_of_hanoi(3, 'A', 'C', 'B')
```
#### Conclusion
Recursion is an essential technique in programming, allowing us to solve complex problems through simpler, more manageable sub-problems. By exploring simple recursive challenges in JavaScript, Ruby, and Python, we've seen how recursion can be implemented effectively in different languages, showcasing its versatility and power.
By practicing these examples, developers can gain a deeper understanding of recursion and its applications, enhancing their problem-solving skills.