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 :: unity oculus vibrate 
Csharp :: EntityFramework: using tables in different scemas 
Csharp :: c# (sharp) varibles 
Csharp :: ascx access parent master page 
Csharp :: how to move the camera rotation in phone in c# by touch 
Csharp :: nullable 
Csharp :: unity how to have multiple headers 
Csharp :: open aspx page c# 
Csharp :: 7485438 
Csharp :: unity set terrain to image 
Csharp :: asp.net Read raw Body 
Csharp :: get path revit linked unload 
Csharp :: unity torque distance joint 
Csharp :: jsonconvert serializeobject and jsonconvert deserialize to list 
Csharp :: populate toolstripitems to combobox 
Csharp :: creating weighted graph in c# 
Csharp :: membership get user id 
Csharp :: how to get the askii code of a char in c# 
Csharp :: resharper render pages folder asp.net core 
Csharp :: params keycord as var name c# 
Csharp :: C# program applies bonus points 
Csharp :: set data annotation in model c# 
Csharp :: ip validation .net core 
Csharp :: c# execute run control panel 
Csharp :: c# file watcher specific file 
Csharp :: how to if i enter 1 go to this program C# 
Csharp :: wpf line intersect rectangle 
Csharp :: vb.net tostring numeric format string 
Csharp :: c# escape quotes 
Csharp :: Create an array with random values c# 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =