Skip to content

Commit

Permalink
Exponentitaion (recursive): Scala implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Sasieta Arana committed Oct 29, 2024
1 parent 2362766 commit 1518fcf
Show file tree
Hide file tree
Showing 2 changed files with 13 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.md">
<img align="center" height="25" src="./logos/scala.svg" />
</a>
</td>
<td> <!-- Kotlin -->
Expand Down
11 changes: 11 additions & 0 deletions src/scala/ExponentiationRecursive.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 1518fcf

Please sign in to comment.