Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O, what is the complexity of summing a series of n numbers?

I always thought the complexity of:

1 + 2 + 3 + ... + n is O(n), and summing two n by n matrices would be O(n^2).

But today I read from a textbook, "by the formula for the sum of the first n integers, this is n(n+1)/2" and then thus: (1/2)n^2 + (1/2)n, and thus O(n^2).

What am I missing here?

like image 802
user1032613 Avatar asked Feb 12 '12 21:02

user1032613


People also ask

What is the complexity of the summation of n number?

The time complexity is the same as the time complexity of the function. We are using for loop in our function, and we already saw that using for loop, N operations are performed to calculate the sum of the first N natural numbers. Thus, the time complexity is O ( N ) O(N) O(N).

What is the big O of summation?

Summations of Big-O There is a summation identity that will make working with big-O notation much easier: if f(i)∈O(ip). This basically says that if you sum up a function which is O(np) then the resulting function will be O(np+1). n2 becomes n3 and so forth.

What is the big O complexity of the function?

Big O notation is used to describe the complexity of an algorithm when measuring its efficiency, which in this case means how well the algorithm scales with the size of the dataset.

What is O and n in time complexity?

Linear Time Complexity: O(n) When time complexity grows in direct proportion to the size of the input, you are facing Linear Time Complexity, or O(n). Algorithms with this time complexity will process the input (n) in “n” number of operations.


2 Answers

The big O notation can be used to determine the growth rate of any function.

In this case, it seems the book is not talking about the time complexity of computing the value, but about the value itself. And n(n+1)/2 is O(n^2).

like image 135
svick Avatar answered Sep 29 '22 04:09

svick


You are confusing complexity of runtime and the size (complexity) of the result.

The running time of summing, one after the other, the first n consecutive numbers is indeed O(n).1

But the complexity of the result, that is the size of “sum from 1 to n” = n(n – 1) / 2 is O(n ^ 2).


1 But for arbitrarily large numbers this is simplistic since adding large numbers takes longer than adding small numbers. For a precise runtime analysis, you indeed have to consider the size of the result. However, this isn’t usually relevant in programming, nor even in purely theoretical computer science. In both domains, summing numbers is usually considered an O(1) operation unless explicitly required otherwise by the domain (i.e. when implementing an operation for a bignum library).

like image 38
Konrad Rudolph Avatar answered Sep 29 '22 03:09

Konrad Rudolph