Skip to content

Commit

Permalink
Merge pull request #361 from paulsasi/feature/exponentiation-recursive
Browse files Browse the repository at this point in the history
Exponentiation (recursive): Scala implementation
  • Loading branch information
kelvins authored Oct 31, 2024
2 parents 2362766 + 5b17ea0 commit 186a5b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- Scala -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/scala/ExponentiationRecursive.scala">
<img align="center" height="25" src="./logos/scala.svg" />
</a>
</td>
<td> <!-- Kotlin -->
Expand Down
15 changes: 15 additions & 0 deletions src/scala/ExponentiationRecursive.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.annotation.tailrec

@tailrec
def exponentiationRecursive(
base: Int,
exponent: Int,
accumulator: Int = 1
): Int = exponent match {
case 0 => accumulator
case _ => exponentiationRecursive(base, exponent - 1, accumulator * base)
}

object Main extends App {
println("5 ^ 3 = " + exponentiationRecursive(5, 3))
}

0 comments on commit 186a5b9

Please sign in to comment.