Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- class MultiplyAndDivideFactorials
- {
- static void Main ()
- {
- checked
- {
- Console.Write("Please, enter a whole number, bigger from 1 for N = ");
- string numberStr = Console.ReadLine();
- int numN = int.Parse(numberStr);
- Console.Write("Enter other whole number, bigger from N for K = ");
- numberStr = Console.ReadLine();
- int numK = int.Parse(numberStr);
- if (numK <= 1 || numN >= numK || numN < 1)
- {
- Console.WriteLine("Error - Invalid Input !!!");
- }
- else
- {
- /* If looked deeply in factorial function formula, we will understand
- * why is not necessarily calculate N! and K! separately.
- * N!=N*(N-1)*(N-2)*...*1 and K!=K*(K-1)*(K-2)*...1; *
- * If K>N, the factorial of (K-N)! is Subset of K!,
- * that's mean: K!=K*(K-1)*(K-2)*...*(K-N+1)*(K-N)!;
- * From all of this follows, to calculate Result from (N!*K!)/(K-N)!,
- * must calculate only multiplication of two expessions:
- * 1) Factorial of (K-N+1)*(K-N+2)*...*(K-1)*K;
- * 2) Factorial of N!
- * Result = 1) * 2)
- * Example: If N=4, K=7, 1)=840, 2)=24, Result=20160 */
- BigInteger factorialKToKminusNplus1 = 1;
- for (int i = numK; i > (numK - numN); i--)
- {
- factorialKToKminusNplus1 *= i;
- }
- BigInteger factorialN = 1;
- for (int j = 1; j <= numN; j++)
- {
- factorialN *= j;
- }
- BigInteger multiplicationResult = factorialKToKminusNplus1 * factorialN;
- Console.WriteLine("The Result of Multiplication and Division of these factorials is:");
- Console.WriteLine("RESULT = {0} !", multiplicationResult);
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement