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)) + } +}