Lattice paths
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?
2x2 matrix Lattice paths. Img courtesy: Project Euler |
Actually if you were to find solution for this problem using iterations you will end up discovering a new formula for this problem. I am assuming that if you have took the challenge of solving some problem(not this one), then you are really smart enough to find a formula for this kind of problem. I seriously suggest you to take some time and find a new formula or something else like that.
Okay lets get back to the problem. Actually formula for this kind of problem was found by Some mathematician already and the solution can be written as follows:
If you were to start from (0,0) and then reach (a,b) along the lattice paths then the number of ways in which you can do that can be given as(a+b)Ca also represented as . This is called as Binomial Coefficient and its expansion can be written as:
If you want to learn more on how I got this formulae then you can see:
Program
The program is a direct implementation of the above formula and we have not at all used any iterations or derived any conclusions from the question given. The program is as follows:
I have used the direct inbuilt factorial function from the math module in python.
This made my work still easier and I wrote the program in few minutes.
The whole code is well commented.
If you want to download the code then you can download it from github gist
Output
Summary
My mind says that I have not really worked hard to find the solution to this problem. But my conscience says that I have worked smartly to find the solution to this problem and I have arrived at a solution. Both of them are correct, but I would like to prefer my conscience talk rather than what my mind says.
Anyways this problem is very very easy once you know the formula. But if you have any doubt or didn't understand anything then please do comment in the comment box below and I will be glad to help you.
Please don't hesitate to comment if you have a different/better solution. Share your code even if it has a longer run time than the above problem. Thinking in a different way is the most important thing rather than the execution time.
You can also contact me if you want.
Thank you. Have a nice day😃.