Mathijs - 4 months ago 20

C# Question

I am doing project euler and i am at problem 15 now, here is a link:

https://projecteuler.net/problem=15 . I am trying to solve this with binomial coefficient. Here is a site that explains it: http://www.mathblog.dk/project-euler-15/ . You can find it at the bottom.

My question is, why is the following code wrong? Since this follows the mathematical algorithm I think: n-k+i/i

`int grid = 20;`

long paths = 1;

for (int i = 0; i < grid; i++)

{

paths *= (grid * 2) - (grid + i)

paths /= (i + 1);

}

Console.WriteLine(paths);

Console.ReadKey();

And why is this code wrong? This is exactly as the mathblog site but in 1 line.

`int grid = 20;`

long paths = 1;

for (int i = 0; i < grid; i++)

{

paths *= ((grid * 2) - i) / (i + 1);

}

Console.WriteLine(paths);

Console.ReadKey();

But why is this code right then? Isnt it the same as the previous code? And it doesn't exactly follow the mathematical algorithm does it? Because it's n-k+i/i, and this code does n-i/i

`int grid = 20;`

long paths = 1;

for (int i = 0; i < grid; i++)

{

paths *= ((grid * 2) - i);

paths /= (i + 1);

}

Console.WriteLine(paths);

Console.ReadKey();

Thnx guys!

Answer

If you want to combain the calculation it should be like this

```
paths = (path *((grid * 2) - i))/(i + 1);
```