Problems > Sum 1 to N > Editorial

Sum 1 to N — Solution & Editorial

Back to the Problem

Use the closed form N(N+1)/2 — a loop also passes but the formula is O(1).

Complexity: O(1)

Watch out: The answer reaches ~5×1017 — use 64-bit arithmetic.