Skip to content

Commit 186a5b9

Browse files
authored
Merge pull request #361 from paulsasi/feature/exponentiation-recursive
Exponentiation (recursive): Scala implementation
2 parents 2362766 + 5b17ea0 commit 186a5b9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ In order to achieve greater coverage and encourage more people to contribute to
460460
</a>
461461
</td>
462462
<td> <!-- Scala -->
463-
<a href="./CONTRIBUTING.md">
464-
<img align="center" height="25" src="./logos/github.svg" />
463+
<a href="./src/scala/ExponentiationRecursive.scala">
464+
<img align="center" height="25" src="./logos/scala.svg" />
465465
</a>
466466
</td>
467467
<td> <!-- Kotlin -->

Diff for: src/scala/ExponentiationRecursive.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.annotation.tailrec
2+
3+
@tailrec
4+
def exponentiationRecursive(
5+
base: Int,
6+
exponent: Int,
7+
accumulator: Int = 1
8+
): Int = exponent match {
9+
case 0 => accumulator
10+
case _ => exponentiationRecursive(base, exponent - 1, accumulator * base)
11+
}
12+
13+
object Main extends App {
14+
println("5 ^ 3 = " + exponentiationRecursive(5, 3))
15+
}

0 commit comments

Comments
 (0)