How to resolve the algorithm Exponentiation operator step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exponentiation operator step by step in the Scala programming language

Table of Contents

Problem Statement

Most programming languages have a built-in implementation of exponentiation.

Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both   intint   and   floatint   variants.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Exponentiation operator step by step in the Scala programming language

Source code in the scala programming language

object Exponentiation {
  import scala.annotation.tailrec
  
  @tailrec def powI[N](n: N, exponent: Int)(implicit num: Integral[N]): N = {
    import num._
    exponent match {
      case 0 => one
      case _ if exponent % 2 == 0 => powI((n * n), (exponent / 2))
      case _ => powI(n, (exponent - 1)) * n
    }
  }
  
  @tailrec def powF[N](n: N, exponent: Int)(implicit num: Fractional[N]): N = {
    import num._
    exponent match {
      case 0 => one
      case _ if exponent < 0 => one / powF(n, exponent.abs)
      case _ if exponent % 2 == 0 => powF((n * n), (exponent / 2))
      case _ => powF(n, (exponent - 1)) * n
    }
  }
  
  class ExponentI[N : Integral](n: N) {
    def \u2191(exponent: Int): N = powI(n, exponent)
  }

  class ExponentF[N : Fractional](n: N) {
    def \u2191(exponent: Int): N = powF(n, exponent)
  }

  object ExponentI {
    implicit def toExponentI[N : Integral](n: N): ExponentI[N] = new ExponentI(n)
  }
  
  object ExponentF {
    implicit def toExponentF[N : Fractional](n: N): ExponentF[N] = new ExponentF(n)
  }
  
  object Exponents {
    implicit def toExponent(n: Int): ExponentI[Int] = new ExponentI(n)
    implicit def toExponent(n: Double): ExponentF[Double] = new ExponentF(n)
  }
}


  @tailrec def powI[N](n: N, exponent: Int, acc:Int=1)(implicit num: Integral[N]): N = {
    exponent match {
      case 0 => acc
      case _ if exponent % 2 == 0 => powI(n * n, exponent / 2, acc)
      case _ => powI(n, (exponent - 1), acc*n)
    }
  }


  

You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Mathematica//Wolfram Language programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Fortran programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the Scheme programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the zkl programming language