2020-06-05 20:53:38 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief [Problem 15](https://projecteuler.net/problem=15) solution
|
2020-06-06 21:51:49 +03:00
|
|
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
2020-06-05 20:53:38 +03:00
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdint.h>
|
2020-03-30 21:49:54 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* At every node, there are 2 possible ways to move -> down or right.
|
2020-05-29 23:23:24 +03:00
|
|
|
* Since it is a square grid, there are in all, 2N steps with N down
|
|
|
|
* and N right options, without preference for order.
|
2020-03-30 21:49:54 +03:00
|
|
|
* Hence, the path can be be traced in N out of 2N number of ways.
|
|
|
|
* This is the same as binomial coeeficient.
|
2020-06-28 22:18:52 +03:00
|
|
|
*/
|
2020-03-30 21:49:54 +03:00
|
|
|
unsigned long long number_of_paths(int N)
|
|
|
|
{
|
|
|
|
unsigned long long path = 1;
|
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
{
|
|
|
|
path *= (N << 1) - i;
|
|
|
|
path /= i + 1;
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
|
2020-03-30 21:49:54 +03:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:38 +03:00
|
|
|
/** Main function */
|
2020-03-30 21:49:54 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int N = 20;
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
N = atoi(argv[1]);
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Number of ways to traverse diagonal of %dx%d grid = %llu\n", N, N,
|
|
|
|
number_of_paths(N));
|
2020-03-30 21:49:54 +03:00
|
|
|
|
|
|
|
return 0;
|
2020-06-05 20:53:38 +03:00
|
|
|
}
|