Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Permutation and Combination in C#

public static class PermutationsAndCombinations
{
    public static long nCr(int n, int r)
    {
        // naive: return Factorial(n) / (Factorial(r) * Factorial(n - r));
        return nPr(n, r) / Factorial(r);
    }

    public static long nPr(int n, int r)
    {
        // naive: return Factorial(n) / Factorial(n - r);
        return FactorialDivision(n, n - r);
    }

    private static long FactorialDivision(int topFactorial, int divisorFactorial)
    {
        long result = 1;
        for (int i = topFactorial; i > divisorFactorial; i--)
            result *= i;
        return result;
    }

    private static long Factorial(int i)
    {
        if (i <= 1)
            return 1;
        return i * Factorial(i - 1);
    }
}

// USAGE
Console.WriteLine(PermutationsAndCombinations.nPr(10, 3));
Console.WriteLine(PermutationsAndCombinations.nCr(10, 3));
Comment

permutation and combination program in c#

// C# program to print all 
// permutations of a given string. 
using System; 
  
class GFG 
{ 
    /** 
    * permutation function 
    * @param str string to 
    calculate permutation for 
    * @param l starting index 
    * @param r end index 
    */
    private static void permute(String str, 
                                int l, int r) 
    { 
        if (l == r) 
            Console.WriteLine(str); 
        else
        { 
            for (int i = l; i <= r; i++) 
            { 
                str = swap(str, l, i); 
                permute(str, l + 1, r); 
                str = swap(str, l, i); 
            } 
        } 
    } 
  
    /** 
    * Swap Characters at position 
    * @param a string value 
    * @param i position 1 
    * @param j position 2 
    * @return swapped string 
    */
    public static String swap(String a, 
                            int i, int j) 
    { 
        char temp; 
        char[] charArray = a.ToCharArray(); 
        temp = charArray[i] ; 
        charArray[i] = charArray[j]; 
        charArray[j] = temp; 
        string s = new string(charArray); 
        return s; 
    } 
  
// Driver Code 
public static void Main() 
{ 
    String str = "ABC"; 
    int n = str.Length; 
    permute(str, 0, n-1); 
} 
} 
  
// This code is contributed by mits
Comment

PREVIOUS NEXT
Code Example
Csharp :: windows form button image size 
Csharp :: C# Read Excel columns header return to list 
Csharp :: windows 10 see how long a program has been running 
Csharp :: wetter warendorf 
Csharp :: how to textbox anywhere on chart in c# 
Csharp :: windows forms tablelayoutpanel scroll 
Csharp :: C# count specific words in string 
Csharp :: Area Of the triangle with condition 
Csharp :: unity screentoworldpoint 
Csharp :: partial mvc 
Csharp :: listview android studio java 
Csharp :: dotnet core webapp 
Csharp :: c# position of character in string 
Csharp :: unity destroy 
Csharp :: c# use enum in class 
Csharp :: it solutions 
Csharp :: c# resize image from byte array 
Csharp :: how to scale text from center in unity 
Csharp :: c# reduce a collection to a string 
Csharp :: string to date 
Html :: calling javascript file in html 
Html :: ion-content background color 
Html :: meta author 
Html :: how to change h1 color in html 
Html :: html filter file upload 
Html :: add favicon to html 
Html :: youtube autoplay video 
Html :: how to change the logo in the title of a webpage 
Html :: instagram post iframe 
Html :: ver pdf en html 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =