public static double factorial(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("n has to be non-negative.");
        }

        double f = 1.0;
        for (int i = 2; i <= n; i++) {
            f *= i;
        }

        return f;
    }
@Test
    public void testFactorial() {
        System.out.println("factorial");
        assertEquals(1.0, MathEx.factorial(0), 1E-7);
        assertEquals(1.0, MathEx.factorial(1), 1E-7);
        assertEquals(2.0, MathEx.factorial(2), 1E-7);
        assertEquals(6.0, MathEx.factorial(3), 1E-7);
        assertEquals(24.0, MathEx.factorial(4), 1E-7);
    }