Skip to content

Instantly share code, notes, and snippets.

@ssshukla26
Last active September 22, 2021 18:57
Show Gist options
  • Save ssshukla26/5edae31abfbacf951cca5b886a3f769f to your computer and use it in GitHub Desktop.
Save ssshukla26/5edae31abfbacf951cca5b886a3f769f to your computer and use it in GitHub Desktop.
Formula to calculate sum between two numbers
# Reference : https://www.quora.com/How-do-you-calculate-the-sum-of-the-numbers-between-x-and-y
# This formula does't depened on which one of a or b is greater
def sumBetweenNums(a,b):
m = abs(a-b+1)
k = a+b
r = (m*k)/2
return r
# OR
# If a > b, else make the formula reverse
def sumBetweenNums(a, b):
return ((a * (a + 1)) - (b * (b+1)) ) // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment