Posted on

Interval sum [a, b]

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

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
	}
}
Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.