# Euler Problem 15¶

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20×20 grid?

We should be able to get this with some thinking. Every move for a $nxn$ grid has to contain n downward and n rightward moves. So in total there are 2n moves. Now we only have to get all permutations available with 2n moves. Normally, we would calculate the factorial, but this only works if all symbols are different. In this case we always only have two symbols for the 2n moves. For each solution there exist a certain number of ways we can create this solution. Let's assume we have two right arrows called (1, 2) and two down arrows called (3, 4). We now have the following options to generate the solution in the upper left corner.

    1 2 3 4
    1 2 4 3
    2 1 3 4
    2 1 4 2

So this means if we calculate the number of ways using the factorial $(2\times2)! = 4! = 24$, four of the solutions are equal which gives us $\frac{24}{4} = 6$. The way to calculate the four should be $2! \times 2! = 4$, so our formula is $\frac{(2\times n)!}{n! \times n!}$. So let's try that.

In [4]:

    def get_number_of_routes(n):
        from math import factorial
        return factorial(2 * n) // (factorial(n) * factorial(n))

    assert(get_number_of_routes(2) == 6)
    assert(get_number_of_routes(20) == 137846528820)
    print(get_number_of_routes(20))

    137846528820

Would actually be interesting to see how this formular changes if we add another dimension. The number of moves would change to $3n$ obviously. And I think the number of permutations changes to remove changes to $n! \times n! \times n! = (n!)^3$.

So the final formula is

$n_{routes} = \frac{(d \times n)!}{(n!)^d}$

where d is the number of dimensions and n is the size of the grid.