- public class IdentiteRemarquable
- {
- /**
- * This method enable to calculate factorial
- * @param
- * n - n!
- */
- public static long factoriel(int n)
- {
- return n < 1 ? 1 : n * factoriel(--n);
- }
-
- /*
- * This method enable to calculate a combinaison with n <= k
- * C (n,k) = k! / (n!(k-n)!)
- */
- public static long combinaison(int n, int k)
- {
- return (factoriel(k) / (factoriel(n) * (factoriel(k-n))));
- }
-
- /**
- * This method enable to calculate (a+b)^n
- * (a+b)^n = Sum( C(n,k) * a^k * b^(n-k) )
- */
- public static long calculate(int a, int b, int n)
- {
- long out = 0;
-
- for (int k = 0; k <= n; k++)
- {
- out += combinaison(k, n) * Math.pow((double)a, (double)k) * Math.pow((double)b, (double)(n - k));
- }
-
- return out;
- }
-
-
- /**
- * Test program
- */
- public static void main(String[] args)
- {
- // factoriel
- int f = 5;
- long resultF = IdentiteRemarquable.factoriel(f);
-
- System.out.println("f(" + f + ") = " + resultF); // fact(5) = 120
-
- // combinaison
- int n = 3;
- int k = 17;
- long c = IdentiteRemarquable.combinaison(n, k);
-
- System.out.println("c(" + n + "," + k + ") = " + c); // C (3,17) = 680
-
- // (a+b)^n
- int a = 5;
- int b = 2;
- int n2 = 2;
- long r = IdentiteRemarquable.calculate(a, b, n2);
-
- System.out.println("(" + a + "+" + b + ")^" + n2 + " = " + r); // (5+2)^2 = 49
-
- System.out.println(IdentiteRemarquable.calculate(12, 7, 10)); // (12 + 7)^10 = ...
-
- }
- }
public class IdentiteRemarquable
{
/**
* This method enable to calculate factorial
* @param
* n - n!
*/
public static long factoriel(int n)
{
return n < 1 ? 1 : n * factoriel(--n);
}
/*
* This method enable to calculate a combinaison with n <= k
* C (n,k) = k! / (n!(k-n)!)
*/
public static long combinaison(int n, int k)
{
return (factoriel(k) / (factoriel(n) * (factoriel(k-n))));
}
/**
* This method enable to calculate (a+b)^n
* (a+b)^n = Sum( C(n,k) * a^k * b^(n-k) )
*/
public static long calculate(int a, int b, int n)
{
long out = 0;
for (int k = 0; k <= n; k++)
{
out += combinaison(k, n) * Math.pow((double)a, (double)k) * Math.pow((double)b, (double)(n - k));
}
return out;
}
/**
* Test program
*/
public static void main(String[] args)
{
// factoriel
int f = 5;
long resultF = IdentiteRemarquable.factoriel(f);
System.out.println("f(" + f + ") = " + resultF); // fact(5) = 120
// combinaison
int n = 3;
int k = 17;
long c = IdentiteRemarquable.combinaison(n, k);
System.out.println("c(" + n + "," + k + ") = " + c); // C (3,17) = 680
// (a+b)^n
int a = 5;
int b = 2;
int n2 = 2;
long r = IdentiteRemarquable.calculate(a, b, n2);
System.out.println("(" + a + "+" + b + ")^" + n2 + " = " + r); // (5+2)^2 = 49
System.out.println(IdentiteRemarquable.calculate(12, 7, 10)); // (12 + 7)^10 = ...
}
}