From df063d2c0084583c165d4b9f17dab79125998466 Mon Sep 17 00:00:00 2001 From: Adelya <65678470+Adelechka@users.noreply.github.com> Date: Thu, 28 Mar 2024 07:43:32 +0300 Subject: [PATCH] added calculateThePermutationOfNObjectsTakenRAtATime in scala (#5577) * checkArmstrongNumber in Scala * CombinationCalculator in Scala * CombinationCalculator in Scala deleted * calculateTheCombinationOfNObjectsTakenRAtATime in Scala added --------- Co-authored-by: Harsh Raj --- ...TheCombinationOfNObjectsTakenRAtATime.scala | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/calculateTheCombinationOfNObjectsTakenRAtATime.scala diff --git a/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/calculateTheCombinationOfNObjectsTakenRAtATime.scala b/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/calculateTheCombinationOfNObjectsTakenRAtATime.scala new file mode 100644 index 000000000..826c3d2c2 --- /dev/null +++ b/program/program/calculate-the-combination-of-n-objects-taken-r-at-a-time/calculateTheCombinationOfNObjectsTakenRAtATime.scala @@ -0,0 +1,18 @@ +import scala.io.StdIn.readInt + +object calculateTheCombinationOfNObjectsTakenRAtATime { + 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)) + } +}