Skip to content

Commit

Permalink
Hopefully summing negative numbers is fixed now
Browse files Browse the repository at this point in the history
  • Loading branch information
theHamdiz committed Feb 2, 2025
1 parent 2e24566 commit 0a298df
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func SumRange[T constraints.Integer](start, end T) T {
start, end = end, start
}

// One formula to rule them all
// The formula is: (end * (end + 1) / 2) - (start * (start - 1) / 2)
// Don't worry if you don't understand it, neither do most people
if start >= 1 {
return Sum(end) - Sum(start-1)
}

return Sum(end) - Sum(start-1)
}

Expand Down

0 comments on commit 0a298df

Please sign in to comment.