Home

C# Example: Permutation

 

Introduction

An ordered arrangement of objects is called a permutation. The number of permutations of r distinct objects out of n distinct objects is represent by the formula:

P(n, r)

This program calculates the permutation:

using System;

public class Program
{
    static long Factorial(long number)
    {
        if( number <= 1 )
            return 1;
        else
            return number * Factorial(number - 1);
    }

    static long Permutation(long n, long r)
    {
        if( r == 0 )
            return 0;
        if( n == 0 )
	        return 0;
        if( (r >= 0) && (r <= n) )
	        return Factorial(n) / Factorial(n - r);
        else
            return 0;
    }
    
    static int Main()
    {
        Console.WriteLine("F(10) = {0}", Factorial(10));
        Console.WriteLine("F(6) = {0}\n", Factorial(6));
        Console.WriteLine("P(10, 4) = {0}\n", Permutation(10, 4));
        return 0;
    }
}

This would produce:

F(10) = 3628800

F(6) = 720

P(10, 4) = 5040

Press any key to continue . . .
   
 

Home Copyright © 2007-2012 FunctionX