From 1518fcf3d13206d95b6e7c8144d833600dd476df Mon Sep 17 00:00:00 2001 From: Paul Sasieta Arana Date: Tue, 29 Oct 2024 12:39:05 +0100 Subject: [PATCH 1/3] Exponentitaion (recursive): Scala implementation --- README.md | 4 ++-- src/scala/ExponentiationRecursive.scala | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/scala/ExponentiationRecursive.scala diff --git a/README.md b/README.md index e780c7e6..fc8ae51c 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..eab9234c --- /dev/null +++ b/src/scala/ExponentiationRecursive.scala @@ -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)) +} From 1815d67f16ab08da4799a474ec18bb5aa9c9c6ab Mon Sep 17 00:00:00 2001 From: Paul Sasieta Arana Date: Tue, 29 Oct 2024 12:41:27 +0100 Subject: [PATCH 2/3] README typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc8ae51c..48882c8c 100644 --- a/README.md +++ b/README.md @@ -460,7 +460,7 @@ In order to achieve greater coverage and encourage more people to contribute to - + From 5b17ea076179f25bf6a8c6edcc90c1832c534c39 Mon Sep 17 00:00:00 2001 From: Paul Sasieta Arana Date: Tue, 29 Oct 2024 12:45:21 +0100 Subject: [PATCH 3/3] Linting fixed --- src/scala/ExponentiationRecursive.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/scala/ExponentiationRecursive.scala b/src/scala/ExponentiationRecursive.scala index eab9234c..58f31682 100644 --- a/src/scala/ExponentiationRecursive.scala +++ b/src/scala/ExponentiationRecursive.scala @@ -1,7 +1,11 @@ import scala.annotation.tailrec @tailrec -def exponentiationRecursive(base: Int, exponent: Int, accumulator: Int = 1): Int = exponent match { +def exponentiationRecursive( + base: Int, + exponent: Int, + accumulator: Int = 1 +): Int = exponent match { case 0 => accumulator case _ => exponentiationRecursive(base, exponent - 1, accumulator * base) }