When i am trying to solve problem on codewars, get this error.
My code passes all the tests but not optimized . Do u have any advice ?
Problem : https://www.codewars.com/kata/525e5a1cb735154b320002c8/train/csharp
C#:
code
>Solution :
the problem is that you are using a brute-force solution! when you have an algorithmic/mathematics problem you should think of finding a pattern that can be represented by a formula. in this case, Triangular numbers are already a known problem and there is a solution to it, check out this (https://en.wikipedia.org/wiki/Triangular_number) for more details
so the solution is simple:
public static int Triangular(int n)
{
// handle the edge case (out-of-range values)
if (n <= 0)
return 0;
// apply the triangular numbers formula:
return (n * (n + 1)) / 2;
}