Sum 1 to N — Solution & Editorial
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.
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.