Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why is summing a list of numbers 1 to N O(N^2) instead of O(N), don't you just iterate through the set of numbers once?

EDIT: Nevermind, I think it means the value of the sum is on the order of n^2



The size of the number (result) is (N(N + 1))/2, the computation of the algorithm is constant. I've never heard anyone discuss the result of the algorithm using Big O.


I think he meant factorial rather than sum.

The sum of the numbers is O(1) - you can calculate it algorithmically.

N*(N+1)/2

Ref: http://www.wikihow.com/Sum-the-Integers-from-1-to-N





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: