Find the Sum of Value in between two numbers

by Francis 2012-12-11 17:17:40

Find the Sum of Value in between two numbers (f,l)
f - First Number
l - Last Number
n - Number of Numbers

Trick 1:
∑(f,l) = [(l2 - f2)+f+l] / 2
Example 1:
∑(5,10) = [(100-25)+5+10] / 2 = [75+15] / 2 = 90/2 = 45

Trick 2:
∑(f,l) = [(l + f) / 2] * (l - f +1)
Example 2:
∑(5,10) = [(10 + 5)/2] * (10 - 5 +1) = [15/2] * (6) = 7.5 * 6 = 45

Trick 3: (No Need Last Number)
n = (l - f + 1)
∑(f,l) = [n / 2] * [2f + (n-1)]
Example 3:
n = (10 - 5 + 1) = 6
∑(5,10) = [6/2] * [(2*5) +(6-1)] = [3] * [10 + 5] = 3 * 15 = 45
1133
like
0
dislike
0
mail
flag

You must LOGIN to add comments