Factorials { n! ~> pronounced n factorial } are used to compute permutations (nPr) and combinations (nCr). A factorial is the result of multiplying a given number of consecutive integers from 1 to the given number. It is written with the exclamation sign: n! and it is defined as
0! = 1
1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 245!
5! = 5 x 4 x 3 x 2 x 1 = 120 ...~> and so on
-----------------------------------
Source Code for calculating n!
-----------------------------------
import static java.lang.System.out;
import java.util.Scanner;
public class factorial
{
public static void main(String[] args)
{
out.print(" Enter your value : ");
Scanner in = new Scanner(System.in);
int input = in.nextInt();
int fac = 1;
for(int num = input; num>=1; num--)
{
fac = fac * num;
}
out.printf(" The factorial of %d = %d" , input, fac);
out.println();
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Permutation ~> nPr = n!/(n-r)!
In the context of counting problems, Permutations is the arrangements where the order is important and repetitions or recurrence is not allowed. The number of permutations of n distinct points taken r at a time is written as nPr. Because the number of objects is being arranged cannot exceed total number available. There are n! (n factorial) permutations of n symbols. A r-permutation of n symbols is a permutation of r of them. There are n!/(n - r)! different r-permutations of n symbols. A permutations represented by nPr and calculated from the formula nPr = n!/(n - r)!
------------------------------------
Source Code for calculating nPr
------------------------------------
import static java.lang.System.out;
import java.util.Scanner;
public class nPr
{
// static variables n for the value of n and ...
static int n,r, n_Sub_r;
public static void main(String[] args)
{
out.print(" Enter the value of n = ");
Scanner in1 = new Scanner(System.in);
int input1 = in1.nextInt();
// saving a copy of input1 which is = n
n = input1;
// calculating n factorial
int nFac = 1;
for(int num1 = n; num1 >=1; num1--)
{
nFac = nFac * num1;
}
out.print("\n Enter the value of r = ");
Scanner in2 = new Scanner(System.in);
int input2 = in2.nextInt();
// saving a copy of input2 which is = r
r = input2;
// calculating the value of n - r as n_Sub_r
n_Sub_r = (n - r);
// calculating n - r factorial
int n_rFac = 1;
for(int num2 = n_Sub_r; num2 >=1; num2--)
{
n_rFac = n_rFac * num2;
}
// calculating (n!/(n-r)!)
int result = (nFac / n_rFac);
out.printf("\n %dP%d = %d\n\n" , n ,r, result);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Combination~> nCr = n!/(n-r)!r!
Combinations is a method of selecting several items or symbols out of a larger group or a data set, where an order does not matter. The Combination represented by nCr and calculated from the formula nCr = n!/(r!(n - r)!). Each r combination can be arranged in r! different ways. Then the number of r-permutations is equal to the number of r combinations times r!
------------------------------------
Source Code for calculating nCr
------------------------------------
import static java.lang.System.out;
import java.util.Scanner;
public class nCr
{
// static variables n for the value of n and ...
static int n,r, n_Sub_r;
public static void main(String[] args)
{
out.print(" Enter the value of n = ");
Scanner in1 = new Scanner(System.in);
int input1 = in1.nextInt();
// saving a copy of input1 which is = n
n = input1;
// calculating n factorial
int nFac = 1;
for(int num1 = n; num1 >=1; num1--)
{
nFac = nFac * num1;
}
out.print("\n Enter the value of r = ");
Scanner in2 = new Scanner(System.in);
int input2 = in2.nextInt();
// saving a copy of input2 which is = r
r = input2;
// calculating the value of (n - r) as n_Sub_r
n_Sub_r = (n - r);
// calculating n - r factorial
int n_rFac = 1;
for(int num2 = n_Sub_r; num2 >=1; num2--)
{
n_rFac = n_rFac * num2;
}
// calculating r factorial
int rFac = 1;
for(int num3 = r; num3 >=1; num3--)
{
rFac = rFac * num3;
}
// calculating (n!/(n-r)!r!)
int result = (nFac / (n_rFac * rFac));
out.printf("\n %dC%d = %d\n\n" , n ,r, result);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How to use? Simple; copy and paste code in any IDE of your choice, save, compile then run. GOOD LUCK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0! = 1
1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 245!
5! = 5 x 4 x 3 x 2 x 1 = 120 ...~> and so on
-----------------------------------
Source Code for calculating n!
-----------------------------------
import static java.lang.System.out;
import java.util.Scanner;
public class factorial
{
public static void main(String[] args)
{
out.print(" Enter your value : ");
Scanner in = new Scanner(System.in);
int input = in.nextInt();
int fac = 1;
for(int num = input; num>=1; num--)
{
fac = fac * num;
}
out.printf(" The factorial of %d = %d" , input, fac);
out.println();
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Permutation ~> nPr = n!/(n-r)!
In the context of counting problems, Permutations is the arrangements where the order is important and repetitions or recurrence is not allowed. The number of permutations of n distinct points taken r at a time is written as nPr. Because the number of objects is being arranged cannot exceed total number available. There are n! (n factorial) permutations of n symbols. A r-permutation of n symbols is a permutation of r of them. There are n!/(n - r)! different r-permutations of n symbols. A permutations represented by nPr and calculated from the formula nPr = n!/(n - r)!
------------------------------------
Source Code for calculating nPr
------------------------------------
import static java.lang.System.out;
import java.util.Scanner;
public class nPr
{
// static variables n for the value of n and ...
static int n,r, n_Sub_r;
public static void main(String[] args)
{
out.print(" Enter the value of n = ");
Scanner in1 = new Scanner(System.in);
int input1 = in1.nextInt();
// saving a copy of input1 which is = n
n = input1;
// calculating n factorial
int nFac = 1;
for(int num1 = n; num1 >=1; num1--)
{
nFac = nFac * num1;
}
out.print("\n Enter the value of r = ");
Scanner in2 = new Scanner(System.in);
int input2 = in2.nextInt();
// saving a copy of input2 which is = r
r = input2;
// calculating the value of n - r as n_Sub_r
n_Sub_r = (n - r);
// calculating n - r factorial
int n_rFac = 1;
for(int num2 = n_Sub_r; num2 >=1; num2--)
{
n_rFac = n_rFac * num2;
}
// calculating (n!/(n-r)!)
int result = (nFac / n_rFac);
out.printf("\n %dP%d = %d\n\n" , n ,r, result);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Combination~> nCr = n!/(n-r)!r!
Combinations is a method of selecting several items or symbols out of a larger group or a data set, where an order does not matter. The Combination represented by nCr and calculated from the formula nCr = n!/(r!(n - r)!). Each r combination can be arranged in r! different ways. Then the number of r-permutations is equal to the number of r combinations times r!
------------------------------------
Source Code for calculating nCr
------------------------------------
import static java.lang.System.out;
import java.util.Scanner;
public class nCr
{
// static variables n for the value of n and ...
static int n,r, n_Sub_r;
public static void main(String[] args)
{
out.print(" Enter the value of n = ");
Scanner in1 = new Scanner(System.in);
int input1 = in1.nextInt();
// saving a copy of input1 which is = n
n = input1;
// calculating n factorial
int nFac = 1;
for(int num1 = n; num1 >=1; num1--)
{
nFac = nFac * num1;
}
out.print("\n Enter the value of r = ");
Scanner in2 = new Scanner(System.in);
int input2 = in2.nextInt();
// saving a copy of input2 which is = r
r = input2;
// calculating the value of (n - r) as n_Sub_r
n_Sub_r = (n - r);
// calculating n - r factorial
int n_rFac = 1;
for(int num2 = n_Sub_r; num2 >=1; num2--)
{
n_rFac = n_rFac * num2;
}
// calculating r factorial
int rFac = 1;
for(int num3 = r; num3 >=1; num3--)
{
rFac = rFac * num3;
}
// calculating (n!/(n-r)!r!)
int result = (nFac / (n_rFac * rFac));
out.printf("\n %dC%d = %d\n\n" , n ,r, result);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How to use? Simple; copy and paste code in any IDE of your choice, save, compile then run. GOOD LUCK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment