From 1dc71abe936c145ea14e6b971b6400758fe9fe4e Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 30 Mar 2020 14:49:54 -0400 Subject: [PATCH 1/2] combinatorial solution --- project_euler/Problem 15/sol1.c | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 project_euler/Problem 15/sol1.c diff --git a/project_euler/Problem 15/sol1.c b/project_euler/Problem 15/sol1.c new file mode 100644 index 000000000..7775a6791 --- /dev/null +++ b/project_euler/Problem 15/sol1.c @@ -0,0 +1,35 @@ +#include +#include +#include + + +/** + * At every node, there are 2 possible ways to move -> down or right. + * Since it is a square grid, there are in all, 2N steps with N down + * and N right options, without preference for order. + * Hence, the path can be be traced in N out of 2N number of ways. + * This is the same as binomial coeeficient. + **/ +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; + } + + return path; +} + +int main(int argc, char **argv) +{ + int N = 20; + + if (argc == 2) + N = atoi(argv[1]); + + printf("Number of ways to traverse diagonal of %dx%d grid = %llu\n", N, N, number_of_paths(N)); + + return 0; +} \ No newline at end of file From 8d76566b2efb6240e15896f99e1aab7131d7cec3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 30 Mar 2020 18:50:59 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5ef054210..c1e87e5de 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -223,6 +223,8 @@ * [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2006/sol.c) * Problem 07 * [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2007/sol.c) + * Problem 15 + * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2015/sol1.c) ## Searching * [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c)