Posted on Leave a comment

Interval sum [a, b]

[https://telegra.ph/UniLecs-127-Intervalnaya-summa-09-16]

[code]
using System;
public class Program
{
public static long SumOfRange(long a, long b)
{
long result = (a + b) * (b – a + 1) / 2;
return result;
}

public static void Main()
{
Console.WriteLine("UniLecs");
Console.WriteLine(string.Format("Answer = {0}", SumOfRange(1, 2))); // 3
Console.WriteLine(string.Format("Answer = {0}", SumOfRange(1, 3))); // 6
Console.WriteLine(string.Format("Answer = {0}", SumOfRange(2, 4))); // 9
}
}
[/code]