In this tutorial, we will learn how to calculate the power of a number using recursion in Swift. We will explore what recursion is, write the algorithm to compute the power of a number, and implement the solution step by step with a complete Swift program and output.
What is Recursion?
Recursion is a process in which a function calls itself to solve smaller subproblems of a larger problem. In recursive functions, a base case defines when to stop the recursion, and the recursive case breaks the problem into smaller instances.
In this tutorial, recursion will be used to calculate the power of a number by breaking it into smaller multiplications.
Algorithm to Calculate Power using Recursion
To compute the power of a number base
raised to an exponent exp
, follow these steps:
- If
exp
is 0, return 1 (base case). - If
exp
is greater than 0, multiplybase
by the result of the same function withexp - 1
. - If
exp
is less than 0, calculate the power for the positive exponent and take its reciprocal (for fractional powers).
This approach ensures that the function handles all cases: positive exponents, negative exponents, and zero.
Step-by-Step Implementation in Swift
Let’s translate the algorithm into Swift code step by step.
1. Handle the Base Case
The base case is when the exponent is 0. Any number raised to the power of 0 is 1:
func power(base: Double, exp: Int) -> Double {
if exp == 0 {
return 1 // Base case: any number to the power of 0 is 1
}
return 0 // Placeholder for further steps
}
2. Handle the Recursive Case for Positive Exponents
If the exponent is positive, multiply the base by the result of the same function with exp - 1
:
func power(base: Double, exp: Int) -> Double {
if exp == 0 {
return 1 // Base case
} else if exp > 0 {
return base * power(base: base, exp: exp - 1) // Recursive case for positive exponents
}
return 0 // Placeholder for further steps
}
3. Handle the Recursive Case for Negative Exponents
If the exponent is negative, calculate the power for the positive exponent and take its reciprocal:
func power(base: Double, exp: Int) -> Double {
if exp == 0 {
return 1 // Base case
} else if exp > 0 {
return base * power(base: base, exp: exp - 1) // Recursive case for positive exponents
} else {
return 1 / power(base: base, exp: -exp) // Recursive case for negative exponents
}
}
Explanation:
if exp == 0
: Stops the recursion when the exponent reaches 0.
else if exp > 0
: Multiplies the base by the result of the same function with the exponent decremented by 1.
else
: Handles negative exponents by computing the reciprocal of the positive exponent result.
4. Test the Function
Let’s test the function with some example inputs:
// Test cases
print("2^3 = \(power(base: 2, exp: 3))") // 8.0
print("5^0 = \(power(base: 5, exp: 0))") // 1.0
print("2^-2 = \(power(base: 2, exp: -2))") // 0.25
The function computes powers for positive, zero, and negative exponents.
Complete Swift Program
Here’s the complete Swift program:
import Foundation
// Function to calculate the power of a number using recursion
func power(base: Double, exp: Int) -> Double {
if exp == 0 {
return 1 // Base case
} else if exp > 0 {
return base * power(base: base, exp: exp - 1) // Recursive case for positive exponents
} else {
return 1 / power(base: base, exp: -exp) // Recursive case for negative exponents
}
}
// Test cases
print("2^3 = \(power(base: 2, exp: 3))") // 8.0
print("5^0 = \(power(base: 5, exp: 0))") // 1.0
print("2^-2 = \(power(base: 2, exp: -2))") // 0.25
print("10^4 = \(power(base: 10, exp: 4))") // 10000.0
Output
2^3 = 8.0
5^0 = 1.0
2^-2 = 0.25
10^4 = 10000.0
Screenshot from Xcode