Skip to content

Commit 83e3758

Browse files
eksperimentaljosevalim
authored andcommitted
Enum.sum/2: Simplified formula for ranges
As suggested by @princemaple in #4414 (comment)
1 parent 58fef1c commit 83e3758

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/elixir/lib/enum.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ defmodule Enum do
14231423
do: sum(last..first)
14241424

14251425
def sum(first..last) when last > first do
1426-
div(((last * (last + 1)) - (first * (first - 1))), 2)
1426+
div((last + first) * (last - first + 1), 2)
14271427
end
14281428

14291429
def sum(enumerable) do

0 commit comments

Comments
 (0)