Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Execution Timed Out (12000 ms) : How can I fix this error

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
  } 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading