Last active
September 22, 2021 18:57
-
-
Save ssshukla26/5edae31abfbacf951cca5b886a3f769f to your computer and use it in GitHub Desktop.
Formula to calculate sum between two numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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