Range Sum

3.6

542 votes
Mathematics, Easy
Problem

You are given an two integers i and j such that i ≤ j.
Task is to find the sum of all integers from i to j (both inclusive).

For example:
i = 3
j = 5
then sum from i to j will be 3+4+5 = 12

Input:
Single line containing two space separated integers i and j.

Output:
Print out the sum of integers from i to j (both inclusive).

Constraints:
0 ≤ i ≤ 1000000000000000000 (1018)
0 ≤ j ≤ 1000000000000000000 (1018)
i ≤ j
Absolute difference between i and j can be upto 1000000000000000000
i.e. |j-i| ≤ 1000000000000000000 (1018)

Subtask 1: (50 points)
0 ≤ i ≤ 1000000
0 ≤ j ≤ 1000000
i ≤ j
Absolute difference between i and j can be upto 1000000
i.e. |j-i| ≤ 1000000

Subtask 2: (50 points)
Original Constraints

Hint:

  • Use mathematics instead of loop to find sum.
  • Use BigInteger class of Java for large number multiplication.
Sample Input
99 1000000000000
Sample Output
500000000000499999995149
Time Limit: 2
Memory Limit: 256
Source Limit:
Contributers:
Editor Image

?