Advertisement
dimipan80

CalculateNCatalanNumber

Mar 24th, 2014
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class CalculateNCatalanNumber
  5. {
  6.     static void Main ()
  7.     {
  8.         checked
  9.         {
  10.             Console.Write("Please, enter a positive whole number in the range [2...99], N = ");
  11.             string numberStr = Console.ReadLine();
  12.             int numN = int.Parse(numberStr);
  13.             if (numN < 2 || numN >= 100)
  14.             {
  15.                 Console.WriteLine("Error - Invalid Number !!!");
  16.             }
  17.             else
  18.             /* The formula for Catalan numbers: C = (2N)! / ((N + 1)! * N!) .
  19.              * Will seeing factorials N! and (N + 1)! They are Subsets of
  20.              * factorial (2N)! That's means, the N Catalan Number can be
  21.              * calculated by other most easy way: */
  22.  
  23.             {
  24.                 BigInteger numberCatalan = 1;
  25.                 for (int i = numN; i > 1; i--)
  26.                 {
  27.                     numberCatalan *= (i + numN) / i;
  28.                 }
  29.  
  30.                 Console.WriteLine("The N-th number of Catalan is CATALAN NUM = {0} !",
  31.                     numberCatalan);
  32.             }            
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement