From abb8efd2eb7fac88ab30dbc82975ee859a9fde27 Mon Sep 17 00:00:00 2001 From: adelasaripova Date: Sat, 23 Mar 2024 23:43:54 +0300 Subject: [PATCH 1/4] checkArmstrongNumber in Scala --- .../checkArmstrongNumber.scala | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 program/program/check-armstrong-number/checkArmstrongNumber.scala diff --git a/program/program/check-armstrong-number/checkArmstrongNumber.scala b/program/program/check-armstrong-number/checkArmstrongNumber.scala new file mode 100644 index 000000000..bd8e26239 --- /dev/null +++ b/program/program/check-armstrong-number/checkArmstrongNumber.scala @@ -0,0 +1,19 @@ +import scala.io.StdIn.readInt + +object checkArmstrongNumber { + def isArmstrongNumber(number: Int): Boolean = { + val digits = number.toString.map(_.asDigit) + val numberOfDigits = digits.length + val sumOfPowers = digits.map(math.pow(_, numberOfDigits)).sum + sumOfPowers == number + } + + def main(args: Array[String]): Unit = { + val number = readInt() + if (isArmstrongNumber(number)) { + println(s"Armstrong Number") + } else { + println(s"Not Armstrong Number") + } + } +} \ No newline at end of file From b1e2f41ea7bee96103c9f24212a06bc7d908bc4d Mon Sep 17 00:00:00 2001 From: adelasaripova Date: Sun, 24 Mar 2024 21:18:28 +0300 Subject: [PATCH 2/4] CombinationCalculator in Scala --- .../CombinationCalculator.scala | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala diff --git a/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala b/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala new file mode 100644 index 000000000..5a0463891 --- /dev/null +++ b/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala @@ -0,0 +1,18 @@ +import scala.io.StdIn.readInt + +object CombinationCalculator { + def main(args: Array[String]): Unit = { + val n = readInt() + val r = readInt() + println(combination(n, r)) + } + + def factorial(n: Int): BigInt = { + if (n == 0) 1 + else n * factorial(n - 1) + } + + def combination(n: Int, r: Int): BigInt = { + factorial(n) / (factorial(r) * factorial(n - r)) + } +} From 9cc852f448b0e94f1aad0a1d1468a1021e0ebc13 Mon Sep 17 00:00:00 2001 From: adelasaripova Date: Sun, 24 Mar 2024 21:20:39 +0300 Subject: [PATCH 3/4] CombinationCalculator in Scala deleted --- .../CombinationCalculator.scala | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala diff --git a/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala b/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala deleted file mode 100644 index 5a0463891..000000000 --- a/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/CombinationCalculator.scala +++ /dev/null @@ -1,18 +0,0 @@ -import scala.io.StdIn.readInt - -object CombinationCalculator { - def main(args: Array[String]): Unit = { - val n = readInt() - val r = readInt() - println(combination(n, r)) - } - - def factorial(n: Int): BigInt = { - if (n == 0) 1 - else n * factorial(n - 1) - } - - def combination(n: Int, r: Int): BigInt = { - factorial(n) / (factorial(r) * factorial(n - r)) - } -} From 8dc751b5d9ea0b82ce115296adf07c30f49f85ec Mon Sep 17 00:00:00 2001 From: adelasaripova Date: Sun, 24 Mar 2024 21:35:50 +0300 Subject: [PATCH 4/4] findDigitalRootOfANumber in Scala added --- .../findDigitalRootOfANumber.scala | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 program/program/find-digital-root-of-a-number/findDigitalRootOfANumber.scala diff --git a/program/program/find-digital-root-of-a-number/findDigitalRootOfANumber.scala b/program/program/find-digital-root-of-a-number/findDigitalRootOfANumber.scala new file mode 100644 index 000000000..69898287e --- /dev/null +++ b/program/program/find-digital-root-of-a-number/findDigitalRootOfANumber.scala @@ -0,0 +1,20 @@ +import scala.annotation.tailrec + +object findDigitalRootOfANumber { + def main(args: Array[String]): Unit = { + val number = 942 + println(digitalRoot(number)) + } + + def sumOfDigits(n: Int): Int = { + if (n == 0) 0 + else n % 10 + sumOfDigits(n / 10) + } + + @tailrec + def digitalRoot(n: Int): Int = { + val sum = sumOfDigits(n) + if (sum < 10) sum + else digitalRoot(sum) + } +}