For 1 to n the formula is sum =n(n+1)/2
, so from n1
to n2
you have to calculate sum of n1-1
terms snd sum of n2
terms and then subtract.
sum_of_n1_to_n2 = n2(n2+1)/2 - (n1-1)((n1-1) +1)/2;
9
solved algorithm for the sum of integer peer from n to n [closed]