w3resource

Kotlin Tail-Recursive function: Calculate the power of a number


Write a Kotlin tail-recursive function to calculate the power of a number.


Pre-Knowledge (Before You Start!)

Before solving this exercise, you should be familiar with the following concepts:

  • Tail Recursion: A function is tail-recursive when the recursive call is the last operation in the function. This allows the function to run efficiently and prevents stack overflow errors even with large input values.
  • Base Case in Recursion: The base case is the condition that stops the recursion. For the power function, the base case is when the exponent is 0, which means the result should be 1.
  • Recursive Case: The recursive case involves calculating the result of a number raised to a power by multiplying the base with the result from the previous recursive call.
  • Exponentiation: Exponentiation is the operation of raising a number (the base) to the power of another number (the exponent). For example, 3 raised to the power of 2 is 3 * 3 = 9.

Hints (Try Before Looking at the Solution!)

Here are some hints to help you solve the problem:

  • Hint 1: The base case for exponentiation is when the exponent is 0. In this case, the result should be 1 because any number raised to the power of 0 is 1.
  • Hint 2: In each recursive call, multiply the current result by the base and decrement the exponent by 1.
  • Hint 3: Use tail recursion by passing the updated result in the recursive call. The result should accumulate the product of the base raised to the current exponent.
  • Hint 4: To avoid stack overflow and optimize the recursion, ensure the multiplication happens before the recursive call without any additional operations after the call.

Sample Solution:

Kotlin Code:

tailrec fun power(base: Int, exponent: Int, result: Int = 1): Int {
    if (exponent == 0) {
        return result
    }
    
    return power(base, exponent - 1, result * base)
}

fun main() {
    val base = 3
    val exponent = 5
    val result = power(base, exponent)
    println("$base raised to the power of $exponent is: $result")
}

Sample Output:

3 raised to the power of 5 is: 243

Explanation:

In the above exercise -

  • The "fibonacci()" function takes three parameters: n, which represents the position of the Fibonacci number to be calculated, a and b which are used to keep track of the previous two Fibonacci numbers.
  • If n becomes 0, we have reached the desired position and return a, the current Fibonacci number.
  • In each recursive call, we decrement n by 1 and calculate the next Fibonacci number by adding the previous two Fibonacci numbers (a + b). The current value of b becomes the new value of a, and the sum a + b becomes the new value of b.
  • By using tail recursion, we make the recursive call directly without any additional computation.
  • The recursive calls continue until the base case is reached and the final Fibonacci number is computed.

Kotlin Editor:


Previous: Calculate the nth Fibonacci.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.