mirror of
https://github.com/TheAlgorithms/C
synced 2025-02-20 15:34:51 +03:00
Merge branch 'project_euler/problem_15' into project_euler/master
# Conflicts: # DIRECTORY.md
This commit is contained in:
commit
772da4e1ac
35
project_euler/Problem 15/sol1.c
Normal file
35
project_euler/Problem 15/sol1.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user