diff --git a/README.md b/README.md index e780c7e6..48882c8c 100644 --- a/README.md +++ b/README.md @@ -460,8 +460,8 @@ In order to achieve greater coverage and encourage more people to contribute to - - + + diff --git a/src/scala/ExponentiationRecursive.scala b/src/scala/ExponentiationRecursive.scala new file mode 100644 index 00000000..58f31682 --- /dev/null +++ b/src/scala/ExponentiationRecursive.scala @@ -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)) +}