C#: o(1) fastest way to sum from 1 to 100000

Posted by Unknown On Friday, June 9, 2017 0 comments

Fast test way to sum from 1 to 100000 by using arithmetic series.


public static int Sum(int value)
{
 var n = value;
 var a1 = 1;
 var an = a1 + (n - 1);
 var sn = n * (a1 + an) / 2;
 return sn;
}

0 comments:

Post a Comment