lmarkov

Divide N Factorial By K Factorial

Dec 10th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Numerics;
  4.  
  5. class DivideNFactorialByKFactorial
  6. {
  7.     static void Main()
  8.     {
  9.         BigInteger numberN, numberK;
  10.         string invalidInput = "Please enter a valid number! (1 < K < N)" + Environment.NewLine;
  11.  
  12.         while (true)
  13.         {
  14.             Stopwatch stopWatch = new Stopwatch();
  15.  
  16.             Console.WriteLine("Enter a value for N: ");
  17.             while (!(BigInteger.TryParse(Console.ReadLine(), out numberN) && numberN > 1))
  18.             {
  19.                 Console.WriteLine(invalidInput);
  20.                 Console.WriteLine("Enter a value for N: ");
  21.             }
  22.  
  23.             Console.WriteLine("Enter a value for K: ");
  24.             while (!(BigInteger.TryParse(Console.ReadLine(), out numberK) && numberK > 1 && numberK < numberN))
  25.             {
  26.                 Console.WriteLine(invalidInput);
  27.                 Console.WriteLine("Enter a value for K: ");
  28.             }
  29.  
  30.             BigInteger factorialDivision = 1;
  31.  
  32.             for (BigInteger i = numberK + 1; i <= numberN; i++)
  33.             {
  34.                 stopWatch.Start();
  35.                 factorialDivision = factorialDivision * i;
  36.             }
  37.            
  38.             Console.WriteLine("{0}! / {1}! = {2}" + Environment.NewLine, numberN, numberK, factorialDivision);
  39.             stopWatch.Stop();
  40.             TimeSpan ts = stopWatch.Elapsed;
  41.             string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  42.             Console.WriteLine("runtime: " + elapsedTime + Environment.NewLine);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment