-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
sol1.c
41 lines (35 loc) · 957 Bytes
/
sol1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* \file
* \brief [Problem 15](https://projecteuler.net/problem=15) solution
* \author [Krishna Vedala](https://github.com/kvedala)
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/**
* 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;
}
/** Main function */
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;
}